Jeho Park, Stephen Park
August 4, 2018
By the end of this this workshop, you will be able to:
IEEE Spectrum: Popular Programming Languages in 2017
Let's get ready for R-ing!
Open your RStudio app. Open your browser.
2+2
[1] 4
x = 2
x+2
[1] 4
# The "<-" operator is the same as "="
y <- x+1 # "<-" is preferred
x+y
[1] 5
x*x # Multiplication
[1] 4
x/y # Division
[1] 0.6666667
x^3 # Exponentiation
[1] 8
2 < 5 # Less Than
[1] TRUE
x >= 2 # Greater Than Or Equal To
[1] TRUE
x == 1 # Equal to. NOTE: This is different from "="
[1] FALSE
x != x # Not Equal to
[1] FALSE
class("hello, world!") # The class function returns the data type of a variable or value.
[1] "character"
class(x == y)
[1] "logical"
a <- c(1,2,3)
b <- c(3:1)
for(i in 1:3) {
print(a[i]+b[i])
}
[1] 4
[1] 4
[1] 4
a + b
[1] 4 4 4
a^2
[1] 1 4 9
abs(-5) # Absolute Value
[1] 5
sqrt(4) # Square Root
[1] 2
sum(a) # Sum
[1] 6
toupper("hello") # To Upper Case
[1] "HELLO"
sd(a) # Can you guess what this funtion calc?
[1] 1
cor(a,b) # How about this?
[1] -1
install.packages('dplyr')
library('dplyr')
# This will search for usage of the hist function
?hist
# This will search packages and functions about histograms
??histogram
names <- c("John", "Kim", "Terry")
ages <- c(21, 34, 16)
male <- c(TRUE, FALSE, TRUE)
data.frame(names, ages, male)
names ages male
1 John 21 TRUE
2 Kim 34 FALSE
3 Terry 16 TRUE
help(CO2)
# The head function shows the first few entries in a dataframe.
head(CO2)
Plant Type Treatment conc uptake
1 Qn1 Quebec nonchilled 95 16.0
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2
# The summary function shows summary statistics.
summary(CO2)
Plant Type Treatment conc
Qn1 : 7 Quebec :42 nonchilled:42 Min. : 95
Qn2 : 7 Mississippi:42 chilled :42 1st Qu.: 175
Qn3 : 7 Median : 350
Qc1 : 7 Mean : 435
Qc3 : 7 3rd Qu.: 675
Qc2 : 7 Max. :1000
(Other):42
uptake
Min. : 7.70
1st Qu.:17.90
Median :28.30
Mean :27.21
3rd Qu.:37.12
Max. :45.50
hist(CO2$uptake) # Use help function for more plotting options
boxplot(CO2$conc)
plot(CO2$uptake, CO2$conc)
Operators that can be used to extract subsets of R objects.
x <- c("a", "b", "c", "c", "d", "a")
x[1]
x[1:4]
x[x > "a"]
u <- x > "a" # what's u here?
u
x[u] # subsetting using a boolean vector
y <- list(foo=x, bar=x[u])
y
y[[1]]
y$bar
co2_10obs <- CO2[<row>, <col>]
co2_Qn3 <- CO2[?, ?]
co2_gt600conc <- subset(CO2, conc > 600)
head(filter(CO2, Treatment=='nonchilled'))
Plant Type Treatment conc uptake
1 Qn1 Quebec nonchilled 95 16.0
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2
select will output only the columns that you choosehead(select(CO2, Plant, conc, uptake), 3)
Plant conc uptake
1 Qn1 95 16.0
2 Qn1 175 30.4
3 Qn1 250 34.8
x <- select(filter(CO2, Treatment=='nonchilled'), Plant, conc, uptake) # OR
y <- CO2 %>% filter(Treatment=='nonchilled') %>% select(Plant, conc, uptake)
x == y
Plant conc uptake
[1,] TRUE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] TRUE TRUE TRUE
[4,] TRUE TRUE TRUE
[5,] TRUE TRUE TRUE
[6,] TRUE TRUE TRUE
[7,] TRUE TRUE TRUE
[8,] TRUE TRUE TRUE
[9,] TRUE TRUE TRUE
[10,] TRUE TRUE TRUE
[11,] TRUE TRUE TRUE
[12,] TRUE TRUE TRUE
[13,] TRUE TRUE TRUE
[14,] TRUE TRUE TRUE
[15,] TRUE TRUE TRUE
[16,] TRUE TRUE TRUE
[17,] TRUE TRUE TRUE
[18,] TRUE TRUE TRUE
[19,] TRUE TRUE TRUE
[20,] TRUE TRUE TRUE
[21,] TRUE TRUE TRUE
[22,] TRUE TRUE TRUE
[23,] TRUE TRUE TRUE
[24,] TRUE TRUE TRUE
[25,] TRUE TRUE TRUE
[26,] TRUE TRUE TRUE
[27,] TRUE TRUE TRUE
[28,] TRUE TRUE TRUE
[29,] TRUE TRUE TRUE
[30,] TRUE TRUE TRUE
[31,] TRUE TRUE TRUE
[32,] TRUE TRUE TRUE
[33,] TRUE TRUE TRUE
[34,] TRUE TRUE TRUE
[35,] TRUE TRUE TRUE
[36,] TRUE TRUE TRUE
[37,] TRUE TRUE TRUE
[38,] TRUE TRUE TRUE
[39,] TRUE TRUE TRUE
[40,] TRUE TRUE TRUE
[41,] TRUE TRUE TRUE
[42,] TRUE TRUE TRUE
arrange() sorts rows by a column# Use desc() to sort descending
CO2 %>% arrange(desc(uptake)) %>% head(3)
Plant Type Treatment conc uptake
1 Qn3 Quebec nonchilled 1000 45.5
2 Qn2 Quebec nonchilled 1000 44.3
3 Qn3 Quebec nonchilled 675 43.9
mutate() creates new variablesCO2 %>% mutate(conc_L = conc / 1000) %>% head()
Plant Type Treatment conc uptake conc_L
1 Qn1 Quebec nonchilled 95 16.0 0.095
2 Qn1 Quebec nonchilled 175 30.4 0.175
3 Qn1 Quebec nonchilled 250 34.8 0.250
4 Qn1 Quebec nonchilled 350 37.2 0.350
5 Qn1 Quebec nonchilled 500 35.3 0.500
6 Qn1 Quebec nonchilled 675 39.2 0.675
group_by() and summarise() work together to aggregate variablesCO2 %>% group_by(Type) %>% summarise(avg_uptake = mean(uptake))
# A tibble: 2 x 2
Type avg_uptake
<fctr> <dbl>
1 Quebec 33.54286
2 Mississippi 20.88333
select(CO2, -Plant)na.rm = TRUE, this will skip any missing values.# You need to put the full path to the file in quotes or change the working directory [getwd(), setwd()]
Births <- read.csv("Births2015.csv")
Auto <- read.csv("Auto.csv")
head(Auto,1): chevrolet chevelle malibusum(Births$births): 3978497boxplot(Auto$mpg)hist(Births$births)Births %>% group_by(wday) %>% summarise(sum=sum(births)) %>% arrange(sum): 638513arrange(Births, births): 12/25/2015cor(Auto$mpg, Auto$cylinders) or plot(Auto$cylinders, Auto$mpg)x <- Auto %>% group_by(cylinders) %>% summarise(mean(mpg))
plot(x$cylinders, x$`mean(mpg)`)
plot(x$cylinders, x$`mean(mpg)`, type='o', xlab="Cylinders", ylab="Average MPG", main="Cylinders vs. Average MPG") # Cleaner Version