I have the following messy data:
df<- tribble(
~food, ~group, ~group_p, ~group2, ~group2_p, ~group3, ~group3_p,
"banana", "fruits", 1, NA, NA, NA, NA,
"rice", "grains", 1, NA, NA, NA, NA,
"mixed_dish1", "nuts", 0.5, "vegetables", 0.25, "fruits", 0.25,
"mixed_dish2", "vegetables", 0.3, "fruits", 0.4, "grains", 0.3,
"tomato", "vegetables", 1, NA, NA, NA, NA,
"apple", "fruits", 1, NA, NA, NA, NA,
"mixed_dish3", "nuts", 0.9, "fruits", 0.1, NA, NA
)
and I would like to convert it to this long format:
df_long <- tribble(
~food, ~group, ~group_p,
"banana", "fruits", 1,
"rice", "grains", 1,
"mixed_dish1", "nuts", 0.5,
"mixed_dish1", "vegetables", 0.25,
"mixed_dish1", "fruits", 0.25,
"mixed_dish2", "vegetables", 0.3,
"mixed_dish2", "fruits", 0.4,
"mixed_dish2", "grains", 0.3,
"tomato", "vegetables", 1,
"apple", "fruits", 1,
"mixed_dish3", "nuts", 0.9,
"mixed_dish3", "fruits", 0.1
)
By splitting the dataframe and binding the rows, I was able to achieve my goal:
df_part1 <- df %>%
select(food,group,group_p)
df_part2 <- df %>%
select(food,group2,group2_p) %>%
rename("group"=group2,
"group_p"=group2_p) %>%
filter(!is.na(group))
df_part3 <- df %>%
select(food,group3,group3_p) %>%
rename(group=group3,group_p=group3_p)%>%
filter(!is.na(group))
df_binded <- bind_rows(df_part1,df_part2,df_part3) %>%
arrange(food)
I wonder if there might be a simpler way to do this by pivot_longer, But didn't managed to find it on my own.