library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.0.5 v dplyr 1.0.3
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
mpg %>%
head()
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa~
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa~
## 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa~
## 4 audi a4 2 2008 4 auto(av) f 21 30 p compa~
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa~
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa~
mpg %>%
group_by(class) %>%
summarise(average = mean(cty))
## # A tibble: 7 x 2
## class average
## * <chr> <dbl>
## 1 2seater 15.4
## 2 compact 20.1
## 3 midsize 18.8
## 4 minivan 15.8
## 5 pickup 13
## 6 subcompact 20.4
## 7 suv 13.5
mpg %>%
group_by(class) %>%
summarise(across(cty,.fns = mean),
.groups = "drop"
)
## # A tibble: 7 x 2
## class cty
## * <chr> <dbl>
## 1 2seater 15.4
## 2 compact 20.1
## 3 midsize 18.8
## 4 minivan 15.8
## 5 pickup 13
## 6 subcompact 20.4
## 7 suv 13.5
mpg %>%
group_by(class) %>%
summarise(mean_city = mean(cty),
std_city = sd(cty))
## # A tibble: 7 x 3
## class mean_city std_city
## * <chr> <dbl> <dbl>
## 1 2seater 15.4 0.548
## 2 compact 20.1 3.39
## 3 midsize 18.8 1.95
## 4 minivan 15.8 1.83
## 5 pickup 13 2.05
## 6 subcompact 20.4 4.60
## 7 suv 13.5 2.42
mpg %>%
group_by(class) %>%
summarise(across(cty,.fns = list(mean = mean, std = sd)),
.groups = "drop")
## # A tibble: 7 x 3
## class cty_mean cty_std
## * <chr> <dbl> <dbl>
## 1 2seater 15.4 0.548
## 2 compact 20.1 3.39
## 3 midsize 18.8 1.95
## 4 minivan 15.8 1.83
## 5 pickup 13 2.05
## 6 subcompact 20.4 4.60
## 7 suv 13.5 2.42
mpg %>%
group_by(class) %>%
summarise(mean_city = mean(cty),
std_city = sd(cty),
mean_hwy = mean(hwy),
std_hwy = sd(hwy),)
## # A tibble: 7 x 5
## class mean_city std_city mean_hwy std_hwy
## * <chr> <dbl> <dbl> <dbl> <dbl>
## 1 2seater 15.4 0.548 24.8 1.30
## 2 compact 20.1 3.39 28.3 3.78
## 3 midsize 18.8 1.95 27.3 2.14
## 4 minivan 15.8 1.83 22.4 2.06
## 5 pickup 13 2.05 16.9 2.27
## 6 subcompact 20.4 4.60 28.1 5.38
## 7 suv 13.5 2.42 18.1 2.98
mpg %>%
group_by(class) %>%
summarise(across(c(cty, hwy),
.fns = list(mean = mean, std = sd)),
.groups = "drop")
## # A tibble: 7 x 5
## class cty_mean cty_std hwy_mean hwy_std
## * <chr> <dbl> <dbl> <dbl> <dbl>
## 1 2seater 15.4 0.548 24.8 1.30
## 2 compact 20.1 3.39 28.3 3.78
## 3 midsize 18.8 1.95 27.3 2.14
## 4 minivan 15.8 1.83 22.4 2.06
## 5 pickup 13 2.05 16.9 2.27
## 6 subcompact 20.4 4.60 28.1 5.38
## 7 suv 13.5 2.42 18.1 2.98
starwars
## # A tibble: 87 x 14
## name height mass hair_color skin_color eye_color birth_year sex gender
## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
## 1 Luke~ 172 77 blond fair blue 19 male mascu~
## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu~
## 3 R2-D2 96 32 <NA> white, bl~ red 33 none mascu~
## 4 Dart~ 202 136 none white yellow 41.9 male mascu~
## 5 Leia~ 150 49 brown light brown 19 fema~ femin~
## 6 Owen~ 178 120 brown, gr~ light blue 52 male mascu~
## 7 Beru~ 165 75 brown light blue 47 fema~ femin~
## 8 R5-D4 97 32 <NA> white, red red NA none mascu~
## 9 Bigg~ 183 84 black light brown 24 male mascu~
## 10 Obi-~ 182 77 auburn, w~ fair blue-gray 57 male mascu~
## # ... with 77 more rows, and 5 more variables: homeworld <chr>, species <chr>,
## # films <list>, vehicles <list>, starships <list>
mean(starwars$height, na.rm = TRUE)
## [1] 174.358
sd(starwars$height, na.rm = TRUE)
## [1] 34.77043
starwars %>%
group_by(species) %>%
summarise(mean(height))
## # A tibble: 38 x 2
## species `mean(height)`
## * <chr> <dbl>
## 1 Aleena 79
## 2 Besalisk 198
## 3 Cerean 198
## 4 Chagrian 196
## 5 Clawdite 168
## 6 Droid NA
## 7 Dug 112
## 8 Ewok 88
## 9 Geonosian 183
## 10 Gungan 209.
## # ... with 28 more rows
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mtcars %>%
group_by(carb) %>%
summarise(across(mpg, mean))
## # A tibble: 6 x 2
## carb mpg
## * <dbl> <dbl>
## 1 1 25.3
## 2 2 22.4
## 3 3 16.3
## 4 4 15.8
## 5 6 19.7
## 6 8 15
mtcars %>%
group_by(carb) %>%
summarise(across(mpg:qsec, mean))
## # A tibble: 6 x 8
## carb mpg cyl disp hp drat wt qsec
## * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 25.3 4.57 134. 86 3.68 2.49 19.5
## 2 2 22.4 5.6 208. 117. 3.70 2.86 18.2
## 3 3 16.3 8 276. 180 3.07 3.86 17.7
## 4 4 15.8 7.2 309. 187 3.60 3.90 17.0
## 5 6 19.7 6 145 175 3.62 2.77 15.5
## 6 8 15 8 301 335 3.54 3.57 14.6
mtcars %>%
group_by(carb) %>%
summarise(across(where(is.numeric), mean))
## # A tibble: 6 x 11
## carb mpg cyl disp hp drat wt qsec vs am gear
## * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 25.3 4.57 134. 86 3.68 2.49 19.5 1 0.571 3.57
## 2 2 22.4 5.6 208. 117. 3.70 2.86 18.2 0.5 0.4 3.8
## 3 3 16.3 8 276. 180 3.07 3.86 17.7 0 0 3
## 4 4 15.8 7.2 309. 187 3.60 3.90 17.0 0.2 0.3 3.6
## 5 6 19.7 6 145 175 3.62 2.77 15.5 0 1 5
## 6 8 15 8 301 335 3.54 3.57 14.6 0 1 5
mtcars %>%
group_by(carb) %>%
summarise(across(mpg:disp, mean),
across(vs, n_distinct))
## # A tibble: 6 x 5
## carb mpg cyl disp vs
## * <dbl> <dbl> <dbl> <dbl> <int>
## 1 1 25.3 4.57 134. 1
## 2 2 22.4 5.6 208. 2
## 3 3 16.3 8 276. 1
## 4 4 15.8 7.2 309. 2
## 5 6 19.7 6 145 1
## 6 8 15 8 301 1