[R] Parsing data with an uneven number of delimiters
jim holtman
jholtman at gmail.com
Thu May 3 21:35:48 CEST 2007
Try this:
> l1<-c("a/a/a","b/bb/bbb","cc/c")
> l2<-strsplit(l1,"/")
> # determine maximum length
> maxLen <- max(sapply(l2, length))
> # pad to maximum length
> do.call('data.frame', lapply(l2, function(x)c(x, rep(NA, maxLen-length(x)))))
c..a....a....a.. c..b....bb....bbb.. c..cc....c...NA.
1 a b cc
2 a bb c
3 a bbb <NA>
>
>
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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list