Skip to content
Prev 79899 / 398502 Next

splitting a character field in R

Here is one additional solution:

read.table(textConnection(sub("abc", " ", B)), fill = TRUE)

It also works if there are more than 2 fields.     If there can
be spaces in the lines then the sub should be modified to
translate "abc" to some unique character not appearing in
the lines and sep= should be added to the read.table call.
Also as.is=TRUE can be added to the read.table call if
its desired to return character rather than factor columns
and col.name= can be added to the read.table call if it
is desired to control the naming of the returned columns.

This solution will also work with more than two fields.
On 10/28/05, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: