1## ---- include = FALSE--------------------------------------------------------- 2knitr::opts_chunk$set( 3 collapse = TRUE, 4 comment = "#>" 5) 6options(tibble.print_max = 10) 7 8## ----setup, message = FALSE--------------------------------------------------- 9library(tidyr) 10library(dplyr) 11library(readr) 12 13## ----------------------------------------------------------------------------- 14relig_income 15 16## ----------------------------------------------------------------------------- 17relig_income %>% 18 pivot_longer(!religion, names_to = "income", values_to = "count") 19 20## ----------------------------------------------------------------------------- 21billboard 22 23## ----------------------------------------------------------------------------- 24billboard %>% 25 pivot_longer( 26 cols = starts_with("wk"), 27 names_to = "week", 28 values_to = "rank", 29 values_drop_na = TRUE 30 ) 31 32## ---- eval = FALSE------------------------------------------------------------ 33# billboard %>% 34# pivot_longer( 35# cols = starts_with("wk"), 36# names_to = "week", 37# names_prefix = "wk", 38# names_transform = list(week = as.integer), 39# values_to = "rank", 40# values_drop_na = TRUE, 41# ) 42 43## ---- eval = FALSE------------------------------------------------------------ 44# billboard %>% 45# pivot_longer( 46# cols = starts_with("wk"), 47# names_to = "week", 48# names_transform = list(week = readr::parse_number), 49# values_to = "rank", 50# values_drop_na = TRUE, 51# ) 52 53## ----------------------------------------------------------------------------- 54who 55 56## ----------------------------------------------------------------------------- 57who %>% pivot_longer( 58 cols = new_sp_m014:newrel_f65, 59 names_to = c("diagnosis", "gender", "age"), 60 names_pattern = "new_?(.*)_(.)(.*)", 61 values_to = "count" 62) 63 64## ---- eval = FALSE------------------------------------------------------------ 65# who %>% pivot_longer( 66# cols = new_sp_m014:newrel_f65, 67# names_to = c("diagnosis", "gender", "age"), 68# names_pattern = "new_?(.*)_(.)(.*)", 69# names_transform = list( 70# gender = ~ readr::parse_factor(.x, levels = c("f", "m")), 71# age = ~ readr::parse_factor( 72# .x, 73# levels = c("014", "1524", "2534", "3544", "4554", "5564", "65"), 74# ordered = TRUE 75# ) 76# ), 77# values_to = "count", 78# ) 79 80## ----------------------------------------------------------------------------- 81family <- tribble( 82 ~family, ~dob_child1, ~dob_child2, ~gender_child1, ~gender_child2, 83 1L, "1998-11-26", "2000-01-29", 1L, 2L, 84 2L, "1996-06-22", NA, 2L, NA, 85 3L, "2002-07-11", "2004-04-05", 2L, 2L, 86 4L, "2004-10-10", "2009-08-27", 1L, 1L, 87 5L, "2000-12-05", "2005-02-28", 2L, 1L, 88) 89family <- family %>% mutate_at(vars(starts_with("dob")), parse_date) 90family 91 92## ----------------------------------------------------------------------------- 93family %>% 94 pivot_longer( 95 !family, 96 names_to = c(".value", "child"), 97 names_sep = "_", 98 values_drop_na = TRUE 99 ) 100 101## ----------------------------------------------------------------------------- 102anscombe 103 104## ----------------------------------------------------------------------------- 105anscombe %>% 106 pivot_longer(everything(), 107 names_to = c(".value", "set"), 108 names_pattern = "(.)(.)" 109 ) %>% 110 arrange(set) 111 112## ----------------------------------------------------------------------------- 113pnl <- tibble( 114 x = 1:4, 115 a = c(1, 1,0, 0), 116 b = c(0, 1, 1, 1), 117 y1 = rnorm(4), 118 y2 = rnorm(4), 119 z1 = rep(3, 4), 120 z2 = rep(-2, 4), 121) 122 123pnl %>% 124 pivot_longer( 125 !c(x, a, b), 126 names_to = c(".value", "time"), 127 names_pattern = "(.)(.)" 128 ) 129 130## ----------------------------------------------------------------------------- 131df <- tibble(id = 1:3, y = 4:6, y = 5:7, y = 7:9, .name_repair = "minimal") 132df 133 134## ----------------------------------------------------------------------------- 135df %>% pivot_longer(!id, names_to = "name", values_to = "value") 136 137## ----------------------------------------------------------------------------- 138df <- tibble(id = 1:3, x1 = 4:6, x2 = 5:7, y1 = 7:9, y2 = 10:12) 139df %>% pivot_longer(!id, names_to = ".value", names_pattern = "(.).") 140 141## ----------------------------------------------------------------------------- 142fish_encounters 143 144## ----------------------------------------------------------------------------- 145fish_encounters %>% pivot_wider(names_from = station, values_from = seen) 146 147## ----------------------------------------------------------------------------- 148fish_encounters %>% pivot_wider( 149 names_from = station, 150 values_from = seen, 151 values_fill = 0 152) 153 154## ----------------------------------------------------------------------------- 155warpbreaks <- warpbreaks %>% as_tibble() %>% select(wool, tension, breaks) 156warpbreaks 157 158## ----------------------------------------------------------------------------- 159warpbreaks %>% count(wool, tension) 160 161## ----------------------------------------------------------------------------- 162warpbreaks %>% pivot_wider(names_from = wool, values_from = breaks) 163 164## ----------------------------------------------------------------------------- 165warpbreaks %>% 166 pivot_wider( 167 names_from = wool, 168 values_from = breaks, 169 values_fn = list(breaks = mean) 170 ) 171 172## ----------------------------------------------------------------------------- 173production <- expand_grid( 174 product = c("A", "B"), 175 country = c("AI", "EI"), 176 year = 2000:2014 177 ) %>% 178 filter((product == "A" & country == "AI") | product == "B") %>% 179 mutate(production = rnorm(nrow(.))) 180production 181 182## ----------------------------------------------------------------------------- 183production %>% pivot_wider( 184 names_from = c(product, country), 185 values_from = production 186) 187 188## ----------------------------------------------------------------------------- 189production %>% pivot_wider( 190 names_from = c(product, country), 191 values_from = production, 192 names_sep = ".", 193 names_prefix = "prod." 194) 195 196production %>% pivot_wider( 197 names_from = c(product, country), 198 values_from = production, 199 names_glue = "prod_{product}_{country}" 200) 201 202## ----------------------------------------------------------------------------- 203us_rent_income 204 205## ----------------------------------------------------------------------------- 206us_rent_income %>% 207 pivot_wider(names_from = variable, values_from = c(estimate, moe)) 208 209## ----------------------------------------------------------------------------- 210contacts <- tribble( 211 ~field, ~value, 212 "name", "Jiena McLellan", 213 "company", "Toyota", 214 "name", "John Smith", 215 "company", "google", 216 "email", "john@google.com", 217 "name", "Huxley Ratcliffe" 218) 219 220## ----------------------------------------------------------------------------- 221contacts <- contacts %>% 222 mutate( 223 person_id = cumsum(field == "name") 224 ) 225contacts 226 227## ----------------------------------------------------------------------------- 228contacts %>% 229 pivot_wider(names_from = field, values_from = value) 230 231## ----------------------------------------------------------------------------- 232world_bank_pop 233 234## ----------------------------------------------------------------------------- 235pop2 <- world_bank_pop %>% 236 pivot_longer(`2000`:`2017`, names_to = "year", values_to = "value") 237pop2 238 239## ----------------------------------------------------------------------------- 240pop2 %>% count(indicator) 241 242## ----------------------------------------------------------------------------- 243pop3 <- pop2 %>% 244 separate(indicator, c(NA, "area", "variable")) 245pop3 246 247## ----------------------------------------------------------------------------- 248pop3 %>% 249 pivot_wider(names_from = variable, values_from = value) 250 251## ----------------------------------------------------------------------------- 252multi <- tribble( 253 ~id, ~choice1, ~choice2, ~choice3, 254 1, "A", "B", "C", 255 2, "C", "B", NA, 256 3, "D", NA, NA, 257 4, "B", "D", NA 258) 259 260## ----------------------------------------------------------------------------- 261multi2 <- multi %>% 262 pivot_longer(!id, values_drop_na = TRUE) %>% 263 mutate(checked = TRUE) 264multi2 265 266## ----------------------------------------------------------------------------- 267multi2 %>% 268 pivot_wider( 269 id_cols = id, 270 names_from = value, 271 values_from = checked, 272 values_fill = FALSE 273 ) 274 275## ----------------------------------------------------------------------------- 276spec <- relig_income %>% build_longer_spec( 277 cols = !religion, 278 names_to = "income", 279 values_to = "count" 280) 281pivot_longer_spec(relig_income, spec) 282 283## ----------------------------------------------------------------------------- 284spec 285 286## ----------------------------------------------------------------------------- 287us_rent_income %>% 288 pivot_wider(names_from = variable, values_from = c(estimate, moe)) 289 290## ----------------------------------------------------------------------------- 291spec1 <- us_rent_income %>% 292 build_wider_spec(names_from = variable, values_from = c(estimate, moe)) 293spec1 294 295## ----------------------------------------------------------------------------- 296spec2 <- spec1 %>% 297 mutate(.name = paste0(variable, ifelse(.value == "moe", "_moe", ""))) 298spec2 299 300## ----------------------------------------------------------------------------- 301pivot_wider_spec(us_rent_income, spec2) 302 303## ----------------------------------------------------------------------------- 304construction 305 306## ----------------------------------------------------------------------------- 307spec <- tribble( 308 ~.name, ~.value, ~units, ~region, 309 "1 unit", "n", "1", NA, 310 "2 to 4 units", "n", "2-4", NA, 311 "5 units or more", "n", "5+", NA, 312 "Northeast", "n", NA, "Northeast", 313 "Midwest", "n", NA, "Midwest", 314 "South", "n", NA, "South", 315 "West", "n", NA, "West", 316) 317 318## ----------------------------------------------------------------------------- 319pivot_longer_spec(construction, spec) 320 321## ----------------------------------------------------------------------------- 322construction %>% 323 pivot_longer_spec(spec) %>% 324 pivot_wider_spec(spec) 325 326