I am having an issue replicating something in pandas to R.
I want to get the duplicated products with the same Order ID together in a new column
This is how it was done in pandas.
I got an error on both. Any help will be applicated.
I took out the spread function and I got this:
No value was in the Group_Product column but just <S3: grouped_df>.
How do I fix it?
CodePudding user response:
What is done in Pandas: grouping by the Order_ID
column, then concatenating all Product
names in those groups. The R dplyr way follows this structure:
library('dplyr')
mtcars <- mtcars %>%
group_by(carb) %>%
mutate(alldisp = paste0(disp, collapse = "; ")) %>%
ungroup()
For your dataset:
library('dplyr')
temp_sales <- all_sales %>%
group_by(Order_ID) %>%
mutate(`Group Product` = paste0(Product, collapse = ", ")) %>%
ungroup()