R programming: string split n number of characters and cbind them in n number of new columns
2
2
Entering edit mode
9.3 years ago
MAPK ★ 2.1k

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 • 4.6k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
4
Entering edit mode
9.3 years ago
MAPK ★ 2.1k

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=""))
ADD COMMENT
0
Entering edit mode
9.2 years ago

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")
ADD COMMENT

Login before adding your answer.

Traffic: 1105 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6