Hello, I am Edwin Reed-Sanchez and I love to program, hack, and tinker with technology. I also enjoy teaching students the basic programming principles that are universal to all programming languages.
For this class, we will be learning R, which is a widely programming language used for statistical and scientific analysis.
Sign up for Replit using your Gmail account.
- Email me at [email protected] with me your username.
- My replit username is: replit.com/@ereedsanchez
Today we will learn about basic programs and syntax used in R.
print(“text”)
#Comments your code
x = 51
print(x)
name = “sam”
print(sam)
X <- 42
print(X);
x <- 9
y <- 3
x <- y
print(x)
#numeric
var1 <- 3.14
#integer
var2 <- 88L
print(var1)
print(var2)
#text
var3 <- "hello"
print(var3)
message <- "This is called \"escaping\"."
print(message)
will return
[1] "This is called \"escaping\"."
—
message <- "This is called \"escaping\"."
cat(message)
will return
[1] "This is called "escaping".
x <- 11
y <- 4
#addition
print(x+y)
#substraction
print(x-y)
#multiplication
print(x*y)
#division
print(x/y)
x <- 11
y <- 4
#exponentation
print(x^y) #or x**y
#modulus (remainder from division)
print(x%%y)
#integer division
print(x%/%y)
Producing Simple Graphs with R
cars <- c(1, 3, 6, 4, 9)
plot(cars, type="o", col="blue")
title(main="Autos", col.main="red", font.main=4)
DataSet: https://docs.google.com/spreadsheets/d/12M3CZlN1M3xWpR7FJZ5BURpqs1sxNTtaHzLc_UfKADc/edit#gid=0
data <- read.csv('IsleData.csv')
print(data)
year <- data[ , c(1)]
wolves <- data[ , c(2)]
moose <- data[ , c(3)]
plot(year, (wolves*20), type="l", col="brown", ylim=c(1,2500))
lines(year, moose, type="l", col="green")
data <- read.csv('IsleData.csv')
# print(data) # See if data is imported
year <- data[ , c(1)]
wolves <- data[ , c(2)]
moose <- data[ , c(3)]
# Wolves Graph
par(mar = c(5, 4, 4, 4) + 0.25)
plot(year, wolves, type="l", col="red", axes=FALSE)
axis(2, ylim=c(1,50), col="red",las=1)
axis(1, ylim=c(1980,2020),col="black",las=1)
box()
# Moose Graph
par(new=TRUE, mar = c(5, 4, 4, 4) + 0.25)
plot(year, moose, type="l", col="green", ylab = "", axes=FALSE)
# New axis
axis(4, ylim=c(1,3000), col="green",las=1)
# Axis label
mtext("Moose", side = 4, line = 3, col = "green")
plot(year, wolves, type="l", col="red", axes=FALSE)
plot(x-axis, y-axis, type="", col="", axes=TRUE/FALSE)
Plot Documentation 1: https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/plot
Plot Documentation 2: https://www.digitalocean.com/community/tutorials/plot-function-in-r
"p" for points, "l" for lines, "b" for both, "c" for the lines part alone of "b", "o" for both ‘overplotted’, "h" for ‘histogram’ like (or ‘high-density’) vertical lines, "s" for stair steps, "S" for other steps, see ‘Details’ below, "n" for no plotting. Plot Documentation 1: https://www.rdocumentation.org/packages/graphics/versions/3.6.2/topics/plot
R also allows combining multiple graphs into a single image for our viewing convenience using the par() function. We only need to set the space before calling the plot function in our graph.
Axis Documentation: https://r-charts.com/base-r/axes/
We will use R! Coding to visualize climate change in NYC.
- Link to data on Google Drive: https://docs.google.com/spreadsheets/d/1XvkapbADZaXl77W0-tYt5y1g-OmhQ_r4B7hJbki4OOY/edit?usp=sharing
You will get a file called: NYC temp 1990-2020 - NYCtemp.csv Rename file to: NYCtemp.csv
- Link to data in github: https://github.com/ereedsanchez/Learning-R-Enviornmental-/blob/main/Lesson-4-NYC-Global-Warming/NYC-temp.csv
Challenge 1: In current graph add additional plot for average yearly tempmax in Red Challenge 2: In current graph add plot for average yearly tempmin in Blue Challenge 1: Create new graph and plot average yearly percipitation. Be sure to label your graphs.