Re: [R] Parsing data with an uneven number of delimiters

From: jim holtman <jholtman_at_gmail.com>
Date: Thu, 03 May 2007 15:35:48 -0400

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?

______________________________________________
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 - 20:15:44 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.