2

Suppose I have a dataframe with a column that looks like this:

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

I want to split it into columns like this:

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

but it comes out looking like this:

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
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • `data.table::setDT(E)[, tstrsplit(V1, "\\s+")]` or `splitstackshape::cSplit(E, "V1", " ")` – David Arenburg Feb 13 '16 at 22:33
  • What's `colsplit2df`? – lukeA Feb 13 '16 at 22:33
  • @lukeA https://trinker.github.io/qdap/colsplit2df.html – Hack-R Feb 13 '16 at 22:34
  • @DavidArenburg I don't think this is a duplicated because the referenced Question was asking about *equal length* strings. They were splitting 1 term into 2 columns for every row. This question is all about varying string lengths. – Hack-R Feb 13 '16 at 22:42
  • It doesn't matter 90% of the solution will work the same by default. You can see the exact same `cSplit` and `tstrsplit` solution there. And this is a dupe of about 100 (at least) similar questions. I really surprised you haven't found this on Google. – David Arenburg Feb 13 '16 at 22:43
  • @DavidArenburg Using the solution to a question like that which was not about different length strings (differing numbers of result columns) is what caused the problem in the first place. This is NOT a duplicate. – Hack-R Feb 13 '16 at 22:45
  • Ok, I've reopened, please don't vandalize your own post – David Arenburg Feb 13 '16 at 22:55

1 Answers1

2

You could do

library(splitstackshape) 
cSplit(E, 1, " ")
#         V1_1  V1_2 V1_3
# 1:         a brown  fox
# 2: something    NA   NA
# 3:      else    NA   NA
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thanks for your solution. I'm sure this will work fine and just a note for future readers that @DavidArenburg provided a `data.table` solution in the comments – Hack-R Feb 13 '16 at 22:37