I have a data frame in which I have an id variable fruit in the example. For some of the other variables, there is only one corresponding value to each id variable taste, ranking while for others, there are multiple color,origin.
I would like to collapse the data frame so that each id variable has one row. For the variables that have multiple values, I could ideally store them as a list.
However, I can't figure out a way to do this. Here is what I tried using summarise and unique for the variables with multiple values. However, I just got back the original data:
df %>%
group_by(fruit) %>%
summarise(ranking = mean(ranking),
taste = first(taste),
origin = unique(origin),
color = unique(color))
example data:
ranking <- c(1, 1, 2, 2, 3)
fruit <- c("apple", "apple", "pear", "pear", "banana")
color <- c("red", "green", "red", "green", "yellow")
taste <- c("good", "good", "good", "good", "okay")
origin <- c("WA", "CA", "OR", "MX", "PR")
df <- data.frame(fruit, ranking, color, taste, origin)
desired data"
ranking <- c(1, 2, 3)
fruit <- c("apple", "pear", "banana")
color <- list((c("red", "green")), (c("red", "green")), (c("yellow")))
taste <- c("good", "good", "okay")
origin <- list(c("WA","CA"), c("OR", "MX"), c("PR"))
desired_df <- data.frame(fruit, ranking, taste, I(color), I(origin))
Is there an easy way to get this transformation done?

