[R] Parsing data with an uneven number of delimiters

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 3 21:51:29 CEST 2007


Try:

read.table(textConnection(l1), sep = "/", fill = TRUE)


On 5/3/07, Kevin Burke <yankeesblow at bellsouth.net> wrote:
> 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.
>



More information about the R-help mailing list