Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split data frame with multiple delimiter using str_split_fixed?

Tags:

r

stringr

How can i split a column separated by multiple delimiter into separate columns in data frame

read.table(text = " Chr  Nm1 Nm2 Nm3
    chr10_100064111-100064134+Nfif   20  20 20
    chr10_100064115-100064138-Kitl   30 19 40
    chr10_100076865-100076888+Tert   60 440 18
    chr10_100079974-100079997-Itg    50 11 23                
    chr10_100466221-100466244+Tmtc3  55 24 53", header = TRUE)


              Chr              gene   Nm1 Nm2 Nm3
    chr10_100064111-100064134 Nfif   20  20 20
    chr10_100064115-100064138 Kitl   30 19 40
    chr10_100076865-100076888 Tert   60 440 18
    chr10_100079974-100079997 Itg    50 11 23 12                
    chr10_100466221-100466244 Tmtc3  55 24 53 12

i used

library(stringr)
df2 <- str_split_fixed(df1$name, "\\+", 2)

I would like to know how can we include both + and - delimiter

like image 216
beginner Avatar asked Dec 15 '22 05:12

beginner


1 Answers

If you're trying to split one column into multiple, tidyr::separate is handy:

library(tidyr)

dat %>% separate(Chr, into = paste0('Chr', 1:3), sep = '[+-]')

#              Chr1      Chr2  Chr3 Nm1 Nm2 Nm3
# 1 chr10_100064111 100064134  Nfif  20  20  20
# 2 chr10_100064115 100064138  Kitl  30  19  40
# 3 chr10_100076865 100076888  Tert  60 440  18
# 4 chr10_100079974 100079997   Itg  50  11  23
# 5 chr10_100466221 100466244 Tmtc3  55  24  53
like image 97
alistaire Avatar answered Dec 21 '22 23:12

alistaire