-2

You have a column with the following items:

df <- data.frame(X1=c('abc','abc','xbc','xbc','ayc','ayc','abz','abz'), stringsAsFactors=FALSE)

Output:

   X1
1 abc
2 abc
3 xbc
4 xbc
5 ayc
6 ayc
7 abz
8 abz

How do you separate the items so that each letter now constitutes a new item in a separate column?

Example:

   X1  X2  X3  X4
1 abc  a   b   c
2 abc  a   b   c
3 xbc  x   b   c
4 xbc  x   b   c
5 ayc  a   y   c
6 ayc  a   y   c
7 abz  a   b   z
8 abz  a   b   z

Any help would be greatly appreciated.

B C
  • 318
  • 3
  • 16
  • 6
    Did you already search through the other 50 million questions just like this one and not find an answer? – Rich Scriven Feb 01 '16 at 20:56
  • Your example data doesn't contain strings (see `str(yourdf$X1)`). – Glen_b Feb 01 '16 at 21:09
  • Hi Glen, I do not understand the importance of what you're saying. Can you please clarify? – B C Feb 01 '16 at 21:14
  • When you create the data frame, you need to include the argument `stringsAsFactors=FALSE` to ensure that strings are encoded as strings, rather than as factors. – eipi10 Feb 01 '16 at 21:16
  • I see, thank you for the clarification. – B C Feb 01 '16 at 21:21
  • 2
    @RichardScriven given that it's actually hard to find (I know how to do it and *I* can't find a duplicate post, though I am sure there will be several) it would be more helpful if you located a duplicate than simply said "go search". It's non-trivial for a beginner. – Glen_b Feb 01 '16 at 21:33
  • One option is to use e.g. `strsplit(df[,1],split=")` and then turn the list into a data frame (e.g. like [here](http://stackoverflow.com/questions/4227223/r-list-to-data-frame)). – slamballais Feb 01 '16 at 21:33
  • Search `[r] split column` for 2055 results. From the answers in the linked duplicate, just change the delimiter from `"|"` to `""` – Rich Scriven Feb 01 '16 at 21:41

1 Answers1

1

Well since I couldn't locate a duplicate with five minutes (actually, nearer to ten now) of searching, here goes.

If you fix up your data frame correctly, this gets you most of the way:

data.frame(mydf$X1,t(simplify2array(strsplit(mydf$X1,""))))

However if your real problem has a really large number of rows, using "t()" may be a bad way to do it.

Glen_b
  • 7,883
  • 2
  • 37
  • 48