[R] Parsing data with an uneven number of delimiters

Kevin Burke yankeesblow at bellsouth.net
Thu May 3 21:14:19 CEST 2007


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



More information about the R-help mailing list