I have a table that (after some initial processing) has multiple rows with the same main identifier but with different column values (either 0 or a value > 0).
Example table with main identifier "produce"
df = data.frame(produce = c("apples","apples", "bananas","bananas"),
grocery1=c(0,1,1,1),
grocery2=c(1,0,1,1),
grocery3=c(0,0,1,1))
###########################
> df
produce grocery1 grocery2 grocery3
1 apples 0 1 0
2 apples 1 0 0
3 bananas 1 1 1
4 bananas 1 1 1
I'd like to collapse (or merge?) rows with the same identifier and retain the non-empty (here, any nonzero values) values in each column
Example desired output
shopping grocery1 grocery2 grocery3
1 apples 1 1 0
2 bananas 1 1 1
Is there a simple function or piping in tidyverse that I am missing and that can handle this?
dplyr::group_by()
plusdplyr::summarise()
– bbiasi