在拆分为列时,使用NA填充较短字符串的列

时间:2016-02-13 22:17:36

标签: r string split qdap

假设我的数据框的列如下所示:

E <- data.frame(
  c("a brown fox"),
  c("something"),
  c("else")
)
E <- as.data.frame(t(E))

                          V1
c..a.brown.fox.. a brown fox
c..something..     something
c..else..               else

我想把它分成这样的列:

V1         V2     V3
a         brown  fox
something NA     NA
else      NA     NA

但看起来像这样:

pacman::p_load(qdap) # or use library() or whatever
colsplit2df(E, sep = " ")
                        X1        X2        X3
c..a.brown.fox..         a     brown       fox
c..something..   something something something
c..else..             else      else      else

1 个答案:

答案 0 :(得分:2)

你可以做到

library(splitstackshape) 
cSplit(E, 1, " ")
#         V1_1  V1_2 V1_3
# 1:         a brown  fox
# 2: something    NA   NA
# 3:      else    NA   NA