Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try dplyr::group_by()

df <- data.frame(
  col1 = c("A", "A", "B", "C", "D", "C"),
  col2 = c("V1", "V2", "V3", "V4", "V5", "V6")
)
library(dplyr)
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   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading