'Transform long format dataframe with multiples sets of columns into wide format
How can you transform a long format dataframe with multiple sets of columns into a wide format?
df <- data.frame(
id = c(1, 2),
uid = c("m1", "m2"),
germ = c(23, 24),
category = c("x1", "x2"),
mineral = c(78, 10))
Names come from uid and category while Values come from germ and mineral. I have tried working on this with the code below but the names are not what I want
out_df <- df %>%
tidyr::pivot_wider(names_from = c(uid, category), values_from = c(germ, mineral))
Here is what I would like my output to look like
out_df <- data.frame(id = c(1, 2),
m1 = c(23, NA),
m2 = c(NA, 24),
x1 = c(78, NA),
x2 = c(NA, 10))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
