Transform dataset in R

I tried to transform my data as follows using R.I tried tidyverse but not luck to get into a single column. I have case such as Col2 has upto 50 records for a col1.

enter image description here

>Solution :

Try dplyr::group_by()

df <- data.frame(
  col1 = c("A", "A", "B", "C", "D", "C"),
  col2 = c("V1", "V2", "V3", "V4", "V5", "V6")
result <- df %>% group_by(col1) %>% summarize(col2 = paste(col2, collapse = ",")) %>% ungroup()

Now the result is what you expected

# A tibble: 4 × 2
  col1  col2 
  <chr> <chr>
1 A     V1,V2
2 B     V3   
3 C     V4,V6
4 D     V5   

Leave a Reply