Skip to content

ereedsanchez/Learning-R-for-Enviornmental-Science

Repository files navigation

Learning R fo Environmental Science @ Belmont

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.

Information about R

Software for Class

Sign up for Replit using your Gmail account.

Lesson 1 - The Basics

Today we will learn about basic programs and syntax used in R.

Print

print(“text”)

Commenting

#Comments your code

Variables using =

x = 51
print(x)
name =sam”
print(sam)

Variables using <-

leftward <- operator

X <- 42
print(X);
x <- 9
y <- 3 
x <- y 
print(x)

Data Types - Numbers

#numeric
var1 <- 3.14

#integer
var2 <- 88L

print(var1)
print(var2)

Data Types - Strings - Simple Text

#text
var3 <- "hello"
print(var3)

Data Types - Strings - quotations

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".

Lesson 2 - Basic Math

Arithmetic
x <- 11
y <- 4

#addition
print(x+y)

#substraction
print(x-y)

#multiplication
print(x*y)

#division
print(x/y)
Arithmetic Operators
x <- 11
y <- 4

#exponentation
print(x^y) #or x**y

#modulus (remainder from division)
print(x%%y)

#integer division
print(x%/%y)

Resources

Producing Simple Graphs with R

Simple Bar Graphs in R

Lesson 2.1 | Basic Graphs

cars <- c(1, 3, 6, 4, 9)
plot(cars, type="o", col="blue")
title(main="Autos", col.main="red", font.main=4)

Lesson 3 | Wolves and Moose Data

DataSet: https://docs.google.com/spreadsheets/d/12M3CZlN1M3xWpR7FJZ5BURpqs1sxNTtaHzLc_UfKADc/edit#gid=0

R! Code

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")

Lesson 4 | Wolves and Moose Data Complex Graphs

Learn

Full R! Wolves and Moose Data Code

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")

Understand

plot

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

type=""

"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

par

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 customization

Axis Documentation: https://r-charts.com/base-r/axes/


Lesson 5

We will use R! Coding to visualize climate change in NYC.

Download NYC Temperature Data

Go to file/download/csv

You will get a file called: 
NYC temp 1990-2020 - NYCtemp.csv Rename file to: NYCtemp.csv



Replit upload NYCtemp.csv
- files/three dots/upload file
Challenge

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.

About

R-Programming Curriculum for Environmental Science Class

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published