'How do I combine activities from multiple rows into one list/cell in R?
I need your help!
Now I have two data frames/.csvs for each of our two customers (please refer to the picture enter image description here) with each action flow. However, since we have many customers to analyze I would like to transform it into a single one with rows each presenting one customer and his or her action flow in a list (please refer now to the third df in the picture enter image description here).
Thank you in advance, Marius
Solution 1:[1]
Check this:
library(dplyr)
p <- 1;
new_col <- c();
for (i in unique(df$Customer_ID)){
a <- subset(df,df$Customer_ID==i)
s <- paste(a$action,sep="",collapse = ",") %>% paste("c(",.,")",sep="")
new_col[p] <- s
p <- p+1
}
new_df <- cbind(unique(df$Customer_ID),s) %>% as.data.frame()
colnames(new_df) <- c("Customer ID","Actions")
head(new_df)
So supose our df is called " df ", which contains " Custumer_ID " and " Actions ". Subset df in unique of Customer_ID values, and then paste all actions of that ID.
NOTE that the desired output class would be a "list", but instead this is a paste function, so a character string may appear instead of a list.
If this is a problem, consider substrating each element in this " character " collapsed with , and assign that element to any component of the disered list.
Save that " character " into a variable, lets say s, and that variable to a vector called new_column. Finally just add the unique of customers ID with each value s and change its column names.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
