1## ---- echo = FALSE, message = FALSE------------------------------------------- 2knitr::opts_chunk$set(collapse = T, comment = "#>") 3options(tibble.print_min = 4L, tibble.print_max = 4L) 4library(dplyr) 5set.seed(1014) 6 7## ----------------------------------------------------------------------------- 8dim(starwars) 9starwars 10 11## ----------------------------------------------------------------------------- 12starwars %>% filter(skin_color == "light", eye_color == "brown") 13 14## ---- eval = FALSE------------------------------------------------------------ 15# starwars[starwars$skin_color == "light" & starwars$eye_color == "brown", ] 16 17## ----------------------------------------------------------------------------- 18starwars %>% arrange(height, mass) 19 20## ----------------------------------------------------------------------------- 21starwars %>% arrange(desc(height)) 22 23## ----------------------------------------------------------------------------- 24starwars %>% slice(5:10) 25 26## ----------------------------------------------------------------------------- 27starwars %>% slice_head(n = 3) 28 29## ----------------------------------------------------------------------------- 30starwars %>% slice_sample(n = 5) 31starwars %>% slice_sample(prop = 0.1) 32 33## ----------------------------------------------------------------------------- 34starwars %>% 35 filter(!is.na(height)) %>% 36 slice_max(height, n = 3) 37 38## ----------------------------------------------------------------------------- 39# Select columns by name 40starwars %>% select(hair_color, skin_color, eye_color) 41# Select all columns between hair_color and eye_color (inclusive) 42starwars %>% select(hair_color:eye_color) 43# Select all columns except those from hair_color to eye_color (inclusive) 44starwars %>% select(!(hair_color:eye_color)) 45# Select all columns ending with color 46starwars %>% select(ends_with("color")) 47 48## ----------------------------------------------------------------------------- 49starwars %>% select(home_world = homeworld) 50 51## ----------------------------------------------------------------------------- 52starwars %>% rename(home_world = homeworld) 53 54## ----------------------------------------------------------------------------- 55starwars %>% mutate(height_m = height / 100) 56 57## ----------------------------------------------------------------------------- 58starwars %>% 59 mutate(height_m = height / 100) %>% 60 select(height_m, height, everything()) 61 62## ----------------------------------------------------------------------------- 63starwars %>% 64 mutate( 65 height_m = height / 100, 66 BMI = mass / (height_m^2) 67 ) %>% 68 select(BMI, everything()) 69 70## ----------------------------------------------------------------------------- 71starwars %>% 72 transmute( 73 height_m = height / 100, 74 BMI = mass / (height_m^2) 75 ) 76 77## ----------------------------------------------------------------------------- 78starwars %>% relocate(sex:homeworld, .before = height) 79 80## ----------------------------------------------------------------------------- 81starwars %>% summarise(height = mean(height, na.rm = TRUE)) 82 83## ---- eval = FALSE------------------------------------------------------------ 84# a1 <- group_by(starwars, species, sex) 85# a2 <- select(a1, height, mass) 86# a3 <- summarise(a2, 87# height = mean(height, na.rm = TRUE), 88# mass = mean(mass, na.rm = TRUE) 89# ) 90 91## ----------------------------------------------------------------------------- 92summarise( 93 select( 94 group_by(starwars, species, sex), 95 height, mass 96 ), 97 height = mean(height, na.rm = TRUE), 98 mass = mean(mass, na.rm = TRUE) 99) 100 101## ---- eval = FALSE------------------------------------------------------------ 102# starwars %>% 103# group_by(species, sex) %>% 104# select(height, mass) %>% 105# summarise( 106# height = mean(height, na.rm = TRUE), 107# mass = mean(mass, na.rm = TRUE) 108# ) 109 110## ----------------------------------------------------------------------------- 111# `name` represents the integer 1 112select(starwars, name) 113select(starwars, 1) 114 115## ----------------------------------------------------------------------------- 116height <- 5 117select(starwars, height) 118 119## ----------------------------------------------------------------------------- 120name <- "color" 121select(starwars, ends_with(name)) 122 123## ----------------------------------------------------------------------------- 124name <- 5 125select(starwars, name, identity(name)) 126 127## ----------------------------------------------------------------------------- 128vars <- c("name", "height") 129select(starwars, all_of(vars), "mass") 130 131## ----------------------------------------------------------------------------- 132df <- starwars %>% select(name, height, mass) 133 134## ----------------------------------------------------------------------------- 135mutate(df, "height", 2) 136 137## ----------------------------------------------------------------------------- 138mutate(df, height + 10) 139 140## ----------------------------------------------------------------------------- 141var <- seq(1, nrow(df)) 142mutate(df, new = var) 143 144## ----------------------------------------------------------------------------- 145group_by(starwars, sex) 146group_by(starwars, sex = as.factor(sex)) 147group_by(starwars, height_binned = cut(height, 3)) 148 149## ----------------------------------------------------------------------------- 150group_by(df, "month") 151 152