Home > Blockchain >  Copying duplicate values into a new column
Copying duplicate values into a new column

Time:05-03

enter image description here

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.

Pandas Code

I did this My first R code

and this Second R code

I got an error on both. Any help will be applicated.

I took out the spread function and I got this:

enter image description here

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()
  • Related