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

How to combine multiple columns in R

Suppose I have this data set:

     age      height       weight
   "1/2/3"  "12/15/18"   "30/40/37"

How can I have a dataset like a dataset below?

   age height weight
    1    12     30
    2    15     40
    3    18     37

And what if my dataset was like this:

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

  name     age      height       weight
 "Jack"  "1/2/3"  "12/15/18"   "30/40/37"

I want to have a data set like below:

    name  age height weight
   "Jack"  1    12     30
   "Jack"  2    15     40
   "Jack"  3    18     37

How can I do this?

>Solution :

If you don’t mind using external package, you can use separate_rows() from the tidyr package.

library(tidyverse)

df %>% separate_rows(-name, sep = "/")

# A tibble: 3 × 4
  name  age   height weight
  <chr> <chr> <chr>  <chr> 
1 Jack  1     12     30    
2 Jack  2     15     40    
3 Jack  3     18     37    
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