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 split elements separated by underscoe in columns names and create

I have the following dataset. As you could see, the columns names are composed by three elements. I would like to separate each of the element separted by the underscore:

structure(list(post_name_mo = c("3433243juhy234_2323526", "3433243juhy234_2323526", 
"3433243juhy234_2323526"), minna_yout_qu = c("3433243juhy234_2323526", 
"3433243juhy234_2323526", "3433243juhy234_2323526"), tour_sima_ta = c("3433243juhy234_2323526", 
"3433243juhy234_2323526", "3433243juhy234_2323526")), class = "data.frame", row.names = c(NA, 
-3L))

in a way that all the first, second and thirs elements will go each of them under a new column (i.e. column1 = post, minna, tour, column2 = name, yout, sima, column3 = mo, qu, ta)

Expected output

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

clm1   clm2  clm3  clm4
post   name  mo    3433243juhy234_2323526 
minna  yout  qu    3433243juhy234_2323526 
tour   sima  ta    3433243juhy234_2323526

Can anyone suggest something functional for this?
Thanks

>Solution :

tidyr does this well:

library(tidyr)
pivot_longer(dat, everything(), values_to = "clm4") |>
  separate(name, into = c("clm1", "clm2", "clm3"), sep = "_")
# # A tibble: 9 × 4
#   clm1  clm2  clm3  clm4                  
#   <chr> <chr> <chr> <chr>                 
# 1 post  name  mo    3433243juhy234_2323526
# 2 minna yout  qu    3433243juhy234_2323526
# 3 tour  sima  ta    3433243juhy234_2323526
# 4 post  name  mo    3433243juhy234_2323526
# 5 minna yout  qu    3433243juhy234_2323526
# 6 tour  sima  ta    3433243juhy234_2323526
# 7 post  name  mo    3433243juhy234_2323526
# 8 minna yout  qu    3433243juhy234_2323526
# 9 tour  sima  ta    3433243juhy234_2323526

Data

dat <- structure(list(post_name_mo = c("3433243juhy234_2323526", "3433243juhy234_2323526", "3433243juhy234_2323526"), minna_yout_qu = c("3433243juhy234_2323526", "3433243juhy234_2323526", "3433243juhy234_2323526"), tour_sima_ta = c("3433243juhy234_2323526", "3433243juhy234_2323526", "3433243juhy234_2323526")), class = "data.frame", row.names = c(NA, -3L))
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