Hi all, Would appreciate help with transforming this: A B C D a 2 1 4 b 3 3 5 into this: A B E a 2 1 a 2 2 a 2 3 a 2 4 b 3 3 b 3 4 b 3 5 (C<=E<=D) Best, Don -- View this message in context: http://r.789695.n4.nabble.com/Expand-dataframe-according-to-limits-defined-per-row-tp3882319p3882319.html Sent from the R help mailing list archive at Nabble.com.
On 07.10.2011 16:02, darkgaze wrote:> Hi all, > > Would appreciate help with transforming this: > > A B C D > a 2 1 4 > b 3 3 5 > > into this: > > A B E > a 2 1 > a 2 2 > a 2 3 > a 2 4 > b 3 3 > b 3 4 > b 3 5 > > (C<=E<=D)do.call(rbind, apply(dat, 1, function(x) data.frame(A=x[1], B=x[2], E=seq(x[3], x[4])))) Uwe Ligges> Best, > Don > > -- > View this message in context: http://r.789695.n4.nabble.com/Expand-dataframe-according-to-limits-defined-per-row-tp3882319p3882319.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.
William Dunlap
2011-Oct-07 18:33 UTC
[R] Expand dataframe according to limits defined per row
> d <- data.frame(A=c("a","b"), B=2:3, C=c(1,3), D=c(4,5)) > lengths <- 1 + d$D - d$C > cbind(d[rep(seq_along(lengths), lengths),c("A","B")], E=unlist(lapply(seq_along(lengths), function(i)seq(from=d$C[i], to=d$D[i]))))A B E 1 a 2 1 1.1 a 2 2 1.2 a 2 3 1.3 a 2 4 2 b 3 3 2.1 b 3 4 2.2 b 3 5 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of darkgaze > Sent: Friday, October 07, 2011 7:03 AM > To: r-help at r-project.org > Subject: [R] Expand dataframe according to limits defined per row > > Hi all, > > Would appreciate help with transforming this: > > A B C D > a 2 1 4 > b 3 3 5 > > into this: > > A B E > a 2 1 > a 2 2 > a 2 3 > a 2 4 > b 3 3 > b 3 4 > b 3 5 > > (C<=E<=D) > > Best, > Don > > -- > View this message in context: http://r.789695.n4.nabble.com/Expand-dataframe-according-to-limits- > defined-per-row-tp3882319p3882319.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.
Dennis Murphy
2011-Oct-07 20:18 UTC
[R] Expand dataframe according to limits defined per row
Here's one way to do it with the plyr package:
library('plyr')
f <- function(df) with(df, data.frame(B = B, E = seq(C, D)))
ddply(d, 'A', f)
A corresponding solution with the data.table package would be
library('data.table')
dt <- data.table(d, key = 'A')
dt[, list(B, E = seq(C, D)), by = 'A']
HTH,
Dennis
On Fri, Oct 7, 2011 at 7:02 AM, darkgaze <donaldngwe at gmail.com>
wrote:> Hi all,
>
> Would appreciate help with transforming this:
>
> A B C D
> a 2 1 4
> b 3 3 5
>
> into this:
>
> A B E
> a 2 1
> a 2 2
> a 2 3
> a 2 4
> b 3 3
> b 3 4
> b 3 5
>
> (C<=E<=D)
>
> Best,
> Don
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Expand-dataframe-according-to-limits-defined-per-row-tp3882319p3882319.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
>