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:
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