This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R programming: string split n number of characters and cbind them in n number of new columns

Hi Guys,

I have this table (mydf) with the column ALT, and I want to split the string in each cell of this column so that there are only one character per cell and cbind them to new column as ALT1, ALT2, ALT3, ..., ALTn based on the number of characters in the cells and get the result table. How can I do this in R? Thank you for your help.

mydf

REF    ALT       GENE
A      GC        MAPK
T      GCA..n    MAP2K

Result:

REF   ALT      ALT1   ALT2   ALTn            GENE
A     GC       G      C                      MAPK
T     GCA..n   G      C      nth character   MAP2K
r

Nice solution! Also have a look at the solution posted by me. Might be useful and efficient for huge datasets.

2 answers

I think I figured it out myself:

library(plyr)
sst <- strsplit(mydf[,"ALT"], "")
additional.cols<-rbind.fill.matrix(lapply(sst, rbind))
colnames(additional.cols)<-c(paste("ALT",sequence(ncol(additional.cols)),sep=""))

Here is another approach

library(dplyr)
library(splitstackshape)

mydf$ALT = as.character(mydf$ALT)
cSplit(data.frame(mydf %>% rowwise() %>%
       mutate(ALT = ALT, ALTn = paste(unlist(strsplit(ALT, "")),
       collapse = ','))), "ALTn", ",")

# result:
#   REF   ALT  GENE ALTn_1 ALTn_2 ALTn_3 ALTn_4 ALTn_5
#1:   A    GC  MAPK      G      C     NA     NA     NA
#2:   T GCAGA MAP2K      G      C      A      G      A

Here is the reproducible sample data (Just copy and paste the text below in R, to check the above code)

mydf = structure(list(REF = structure(1:2, .Label = c("A", "T"), class = "factor"),
    ALT = c("GC", "GCAGA"), GENE = structure(c(2L, 1L), .Label = c("MAP2K",
    "MAPK"), class = "factor")), .Names = c("REF", "ALT", "GENE"
), row.names = c(NA, -2L), class = "data.frame")

Log in to answer this question.