[R] Parsing data with an uneven number of delimiters

From: Kevin Burke <yankeesblow_at_bellsouth.net>
Date: Thu, 03 May 2007 14:14:19 -0500


I have a list of data that is delimited by a / and, as long as there is an equal number of delimiters, I can parse the data and put it into a data frame:

> t1<-c("a/a/a","b/bb/bbb","ccc/cc/c")
> t2<-strsplit(t1,"/")
> t3<-data.frame(t2)
> t3
  c..a....a....a.. c..b....bb....bbb.. c..ccc....cc....c..

1                a                   b                 ccc
2                a                  bb                  cc
3                a                 bbb                   c

However, if I don't have an equal number of delimiters, this technique doesn't work:

> l1<-c("a/a/a","b/bb/bbb","cc/c")
> l2<-strsplit(l1,"/")
> l3<-data.frame(l2)

Error in data.frame(c("a", "a", "a"), c("b", "bb", "bbb"), c("cc", "c" :

        arguments imply differing number of rows: 3, 2

Is there an easy way to get this into a data frame with NA's (or something else) where the missing data would be?

Thanks in advance.

Kevin Burke



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 03 May 2007 - 19:27:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 May 2007 - 21:32:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.