I want to recursively add text from one row to another using information stored in another column. Below is a sample dataset:
df <- data_frame(person = c(rep("A",4),rep("B",3)),
meal = c(seq(1:4),seq(1:3)),
food = c("Chicken", "Beef", "Soup and meal 2", "Lamb and meal 3",
"Lamb","Salad and meal 1","Beef"),
dependencies = c(NA,NA,2,3,NA,1,NA),
solo_meal = c(1,1,0,1,1,0,1))
Which Gives:
# A tibble: 7 x 5
person meal food dependencies solo_meal
<chr> <int> <chr> <dbl> <dbl>
1 A 1 Chicken NA 1
2 A 2 Beef NA 1
3 A 3 Soup and meal 2 2 0
4 A 4 Lamb and meal 3 3 1
5 B 1 Lamb NA 1
6 B 2 Salad and meal 1 1 0
7 B 3 Beef NA 1
I want to add a column (preferably using tidyverse) that combines the food column to look like this:
combined_meal
<chr>
1 Chicken
2 Beef
3 Soup and Beef
4 Lamb and Soup and Beef
5 Lamb
6 Salad and Lamb
7 Beef
I am thinking that this needs to be done recursively, but I don't know how to do that in the tidyverse (or base for that matter).
Thanks for the help!