I have a data frame that looks like this:
a b
1 x 8
2 x 6
3 y 3
4 y 4
5 z 5
6 z 6
and I want to turn it into this:
x y
Since tidyr 1.0.0 you can use pivot_wider(), and because a doesn't have unique values you'll need a call to unchop on top :
library(tidyr)
df <- data.frame(
a = c("x", "x", "y", "y", "z", "z"),
b = c(8, 6, 3, 4, 5, 6)
)
pivot_wider(df, names_from = "a", values_from = "b", values_fn = list(b = list)) %>%
unchop(everything())
#> # A tibble: 2 x 3
#> x y z
#>
#> 1 8 3 5
#> 2 6 4 6
Created on 2019-09-14 by the reprex package (v0.3.0)