DATA 606 - Statistics & Probability - Fall 2022

Summarizing Data Part 2

Click here to open the slides (PDF).

library(magrittr)
library(dplyr)

data(mtcars)
str(mtcars)

mtcars$gear %>%
    table() %>%
    prop.table()

`%>%`(mtcars$gear, table)

`+`
2 + 3
`+`(2, 3)

`+` <- function(e1, e2) {
    print('Happy April Fools!')
}

2 + 3

a <- 2 + 4
a = 2 + 4
a

tab <- mtcars$gear %>%
    table() %>%
    prop.table()
tab

mtcars$gear %>%
    table() %>%
    prop.table() -> 
    tab
tab

letters %in% c('a','e','i','o','u')
letters %in% c('a','e','i','o','u') %>% which()

mtcars %>%
    filter(mpg > 20 & cyl == 6) %>%
    select(mpg, wt)

filter(mtcars, mpg > 20 & cyl == 6) %>% select(mpg, wt)

head(mtcars, n = 3)
tail(mtcars)

mtcars %>% head(n = 3) %>% rename(miles_per_gallon = mpg)
mtcars2 <- mtcars %>% rename(miles_per_gallon = mpg)
head(mtcars2)

mtcars2[1, 'miles_per_gallon'] <- 0
mtcars2$cyl / mtcars2$miles_per_gallon

mtcars2[1, 'miles_per_gallon'] <- NA
mean(mtcars2$miles_per_gallon)
mean(mtcars2$miles_per_gallon, na.rm = TRUE)

4 / 2
4 / NA
NA / 4
4 / 0 # Why is this Inf
0 / 4