简体   繁体   中英

Splitting a vector of strings to a dataframe with columns containing the respective characters

As a variant of this question

I have a vector with strings, each string has 2 to 4 characters.

Strng <- c("XDX",  "GUV",  "FQ",   "ACUE", "HIT",  "AYX",  "NFD",  "AHBW", "GKQ",  "PYF")

I want to split it to data frame with 4 columns, where each column contains one of the characters or 0 (for the case where the length of the string is less tan 4). The zeros can be in front of - doesn't matter.

So (probably) after applying this:

ss<-strsplit(Strng,"")
z<-lapply(ss,as.character)

I would like to have a dataframe like this:

>df  
"X" "D" "X" "0"  
"G" "U" "V" "0"  
"F" "Q" "0" "0"   
"A" "C" "U" "E" 
"H" "I" "T" "0"  
"A" "Y" "X" "0"  
"N" "F" "D" "0"  
"A" "H" "B" "W" 
"G" "K" "Q" "0"  
"P" "Y" "F" "0"

Any ideas?

Thank you,

Kalin

Here's an alternative with "data.table":

library(data.table)
setDT(tstrsplit(Strng, "", fill = "0"))[]
#     V1 V2 V3 V4
#  1:  X  D  X  0
#  2:  G  U  V  0
#  3:  F  Q  0  0
#  4:  A  C  U  E
#  5:  H  I  T  0
#  6:  A  Y  X  0
#  7:  N  F  D  0
#  8:  A  H  B  W
#  9:  G  K  Q  0
# 10:  P  Y  F  0

You could also use cSplit from my "splitstackshape" package, but it fills with NA and uses a little bit strange syntax:

library(splitstackshape)
cSplit(data.table(Strng), "Strng", "", stripWhite = FALSE)

We can use stri_list2matrix from stringi after we split the "Strng" to a list .

library(stringi)
stri_list2matrix(strsplit(Strng, ''), fill=0, byrow=TRUE)
#     [,1] [,2] [,3] [,4]
# [1,] "X"  "D"  "X"  "0" 
# [2,] "G"  "U"  "V"  "0" 
# [3,] "F"  "Q"  "0"  "0" 
# [4,] "A"  "C"  "U"  "E" 
# [5,] "H"  "I"  "T"  "0" 
# [6,] "A"  "Y"  "X"  "0" 
# [7,] "N"  "F"  "D"  "0" 
# [8,] "A"  "H"  "B"  "W" 
# [9,] "G"  "K"  "Q"  "0" 
#[10,] "P"  "Y"  "F"  "0" 

Or a base R option would be (variant of the one described in the link )

read.fwf(file= textConnection(Strng), 
             widths = rep(1,max(nchar(Strng))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM