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

Split data frame string column and count items. (dplyr and R)

My data looks like this. What I would like to do is split the core_enrchiment column items connected by "/" and count how many IDs (e.g. 101739, 20382, 13006 …) in each row.

> dat %>% select(ID, core_enrichment)
# A tibble: 22 x 2
   ID                           core_enrichment                                                                                                              
   <chr>                        <chr>                                                                                                                        
 1 HALLMARK_E2F_TARGETS         101739/20382/13006/212377/114714/66622/140917/19139/18813/16647/20492/67241/103573/67054/19385/14852/12567/70699/20842/70472…
 2 HALLMARK_G2M_CHECKPOINT      75717/103573/14852/18141/12567/26429/20842/17975/12545/20641/21781/19357/17216/15331/12615/107823/13555/56403/26554/11991/77…
 3 HALLMARK_MYC_TARGETS_V1      66942/56200/27041/12729/68981/20810/27050/19934/110639/66235/12237/70316/26965/109801/12785/103136/11757/16211/18673/20462/1…
 4 HALLMARK_INTERFERON_GAMMA_R… 14293/12575/246728/12265/12984/16149/14969/17329/17750/626578/14129/21928/99899/231655/17858/66141/57444/14960/100121/80876/…
 5 HALLMARK_TNFA_SIGNALING_VIA… 14282/12977/19252/16476/14281/12575/21926/15200/22151/17872/21928/21664/14345/15980/13653/20303/12515/11852/74646/18227/7171…
 6 HALLMARK_P53_PATHWAY         71839/12579/12795/27280/12606/16476/14281/12578/12575/15368/15200/11820/19734/17872/19143/16450/56312/71712/22337/64058/1660…
 7 HALLMARK_SPERMATOGENESIS     17344/15512/23885/12326/71838/18952/15925/14056/16162/27214/20496/18551/21821/20878/12442/106344/22137/53604/215387/72391/73…
 8 HALLMARK_INFLAMMATORY_RESPO… 19222/192187/216799/14293/12977/12986/19204/12575/12267/15200/17329/19734/13733/13136/15980/20288/19217/13058/12515/16402/25…
 9 HALLMARK_MITOTIC_SPINDLE     21844/233406/110033/12190/240641/26934/236266/56699/105988/16906/71819/67052/12488/67141/229841/20878/18817/208084/17318/218…
10 HALLMARK_IL6_JAK_STAT3_SIGN… 12977/12986/16476/15368/12768/21926/12984/17329/94185/16161/15980/16994/16169/12702/12982/21938/18712/16416/15945/12491/1618…

What I did is the codes below and it worked for me.

dat_tmp_df <- dat %>% mutate(tmp_n_genes = str_split(core_enrichment, "/"))
dat_tmp_df$num_genes <- lapply(dat_tmp_df$tmp_n_genes, length) %>% unlist()

> dat_tmp_df %>% select(ID, core_enrichment, num_genes)
# A tibble: 22 x 3
   ID                          core_enrichment                                                                                                      num_genes
   <chr>                       <chr>                                                                                                                    <int>
 1 HALLMARK_E2F_TARGETS        101739/20382/13006/212377/114714/66622/140917/19139/18813/16647/20492/67241/103573/67054/19385/14852/12567/70699/20…       131
 2 HALLMARK_G2M_CHECKPOINT     75717/103573/14852/18141/12567/26429/20842/17975/12545/20641/21781/19357/17216/15331/12615/107823/13555/56403/26554…       102
 3 HALLMARK_MYC_TARGETS_V1     66942/56200/27041/12729/68981/20810/27050/19934/110639/66235/12237/70316/26965/109801/12785/103136/11757/16211/1867…       122
 4 HALLMARK_INTERFERON_GAMMA_… 14293/12575/246728/12265/12984/16149/14969/17329/17750/626578/14129/21928/99899/231655/17858/66141/57444/14960/1001…        84
 5 HALLMARK_TNFA_SIGNALING_VI… 14282/12977/19252/16476/14281/12575/21926/15200/22151/17872/21928/21664/14345/15980/13653/20303/12515/11852/74646/1…        55
 6 HALLMARK_P53_PATHWAY        71839/12579/12795/27280/12606/16476/14281/12578/12575/15368/15200/11820/19734/17872/19143/16450/56312/71712/22337/6…        39
 7 HALLMARK_SPERMATOGENESIS    17344/15512/23885/12326/71838/18952/15925/14056/16162/27214/20496/18551/21821/20878/12442/106344/22137/53604/215387…        28
 8 HALLMARK_INFLAMMATORY_RESP… 19222/192187/216799/14293/12977/12986/19204/12575/12267/15200/17329/19734/13733/13136/15980/20288/19217/13058/12515…        51
 9 HALLMARK_MITOTIC_SPINDLE    21844/233406/110033/12190/240641/26934/236266/56699/105988/16906/71819/67052/12488/67141/229841/20878/18817/208084/…        38
10 HALLMARK_IL6_JAK_STAT3_SIG… 12977/12986/16476/15368/12768/21926/12984/17329/94185/16161/15980/16994/16169/12702/12982/21938/18712/16416/15945/1…        25

I am wondering if there’s more elegant way of doing this using dplyr. My codes worked but look like a spaghetti code.

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 :

You could use the following solution:

library(dplyr)
library(stringr)

df %>%
  mutate(count_str = str_count(core_enrichment, "\\d+"))

# A tibble: 2 x 3
# Rowwise: 
  ID                     core_enrichment                                        count_str
  <chr>                  <chr>                                                      <int>
1 HALLMARK_E2F_TARGETS   101739/20382/13006/212377/114714/66622/140917                  7
2 ALLMARK_G2M_CHECKPOINT 75717/103573/14852/18141/12567/26429/20842/17975/12545         9

Data

structure(list(ID = c("HALLMARK_E2F_TARGETS", "ALLMARK_G2M_CHECKPOINT"
), core_enrichment = c("101739/20382/13006/212377/114714/66622/140917", 
"75717/103573/14852/18141/12567/26429/20842/17975/12545")), class = "data.frame", row.names = c(NA, 
-2L))
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