Pivot_wider - dsofe/coderr GitHub Wiki

pivot_wider() using id_cols = (as grouping var, similar to group_by())

test <- warpbreaks

test1 <- test %>%
  mutate(id = row_number()) %>%
  relocate(id) %>% # best for "pseudo-random" ids
  relocate(tension, .after = "id")
  
test2 <- test1 %>%
  slice_tail(n = 15)

test3 <- test2 %>%
  pivot_wider(id_cols = id, names_from = wool, values_from = breaks)
# not definitive


test4 <- test1 %>%
  pivot_wider(id_cols = id, names_from = wool, values_from = breaks)
# `tension` col dropped
# H1: because it wasn't specified as a var to keep, and therefore be useful, as a grouping var.
# `test5` tests this theory

test5 <- test1 %>%
  pivot_wider(id_cols = c(id, tension), names_from = wool, values_from = breaks)
# GOOD check
⚠️ **GitHub.com Fallback** ⚠️