df <- data.table(
a = rep(c("a", "b", "c"), 2),
b = c(rep("x", 3), rep("y", 3)),
vals = 1:6
)
df %>%
pivot_wider.(names_from = b, values_from = vals)
df %>%
pivot_wider.(
names_from = b, values_from = vals, names_prefix = "new_"
)
Run the code above in your browser using DataLab