I am trying to apply different functions to different columns for a grouped data, but I am unsure why one of the functions isn't working. I did research this topic on R - Group data but apply different functions to different columns this thread, but I believe they are not using lapply
in that example. In my example, I want to concatenate all ID
and apply sum
to all numeric columns.
Here's my Input data:
dput(Input)
structure(list(ID = c(1, 2, 3, 4, 5, 6), Name = c("Boston", "Boston",
"Boston", "Boston", "Seattle", "Washington"), Total_Groceries = c(35,
70, 95, 120, 10, 20), Apple = c(5, 10, 15, 20, 2, 10), Banana = c(10,
20, 30, 40, 5, 8), Pineapple = c(20, 40, 50, 60, 3, 2)), .Names = c("ID",
"Name", "Total_Groceries", "Apple", "Banana", "Pineapple"), row.names = c(NA,
6L), class = "data.frame")
Here's my code:
Input<-data.table::as.data.table(Input)
Input[,as.list(ID=paste0(ID,collapse=";"),unlist(lapply(.SD, sum, na.rm=TRUE))),by=.(Name),.SDcols=c("Total_Groceries","Apple","Banana","Pineapple")]
Running above code, you would see that I am not getting concatenated string for ID
Here's expected output:
dput(Output)
structure(list(ID = c("1;2;3;4", "5", "6"), Name = c("Boston",
"Seattle", "Washington"), Total_Groceries = c(320, 10, 20), Apple = c(50,
2, 10), Banana = c(100, 5, 8), Pineapple = c(170, 3, 2)), .Names = c("ID",
"Name", "Total_Groceries", "Apple", "Banana", "Pineapple"), row.names = c(NA,
3L), class = "data.frame")
As a bonus (and for my learning, it would really help if you could post an answer based on dplyr
. I am primarily looking for data.table
based answer because of the sheer size of original data.
I'd appreciate any thoughts.