Re: [R] [Rd] reshape scaling with large numbers of times/rows

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 25 Aug 2006 - 01:15:51 EST

On 8/24/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> Here is one more solution . It uses the reshape package.
> Its faster than using reshape but not as fast as xtabs;
> however, it is quite simple -- just one line and if that
> matters it might be useful:
>
> library(reshape)
> system.time(w4 <- cast(melt(DF, id = 1:2), Y ~ X, head, n = 1))
>
> On 8/24/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> > On 8/24/06, Mitch Skinner <mitch@gallo.ucsf.edu> wrote:
> > > On Thu, 2006-08-24 at 08:57 -0400, Gabor Grothendieck wrote:
> > > > If your Z in reality is not naturally numeric try representing it as a
> > > > factor and using
> > > > the numeric levels as your numbers and then put the level labels back on:
> > > >
> > > > m <- n <- 5
> > > > DF <- data.frame(X = gl(m*n, 1), Y = gl(m, n), Z = letters[1:25])
> > > > Zn <- as.numeric(DF$Z)
> > > > system.time(w1 <- reshape(DF, timevar = "X", idvar = "Y", dir = "wide"))
> > > > system.time({Zn <- as.numeric(DF$Z)
> > > > w2 <- xtabs(Zn ~ Y + X, DF)
> > > > w2[w2 > 0] <- levels(DF$Z)[w2]
> > > > w2[w2 == 0] <- NA
> > > > })
> > >
> > > This is pretty slick, thanks. It looks like it works for me. For the
> > > archives, this is how I got back to a data frame (as.data.frame(w2)
> > > gives me a long version again):
> > >
> > > > m <- 4500
> > > > n <- 70
> > > > DF <- data.frame(X = gl(m, n), Y = 1:n, Z = letters[1:25])
> > > > system.time({Zn <- as.numeric(DF$Z)
> > > + w2 <- xtabs(Zn ~ Y + X, DF)
> > > + w2[w2 > 0] <- levels(DF$Z)[w2]
> > > + w2[w2 == 0] <- NA
> > > + WDF <- data.frame(Y=dimnames(w2)$Y)
> > > + for (col in dimnames(w2)$X) { WDF[col]=w2[,col] }
> > > + })
> > > [1] 131.888 1.240 135.945 0.000 0.000
> > > > dim(WDF)
> > > [1] 70 4501
> > >
> > > I'll have to look; maybe I can just use w2 as is. Next time I guess
> > > I'll try R-help first.
> > >
> > > Thanks again,
> > > Mitch
> > >
> >
> > Also try
> > na.omit(as.data.frame(w2))
> >
>



R-help@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 Fri Aug 25 01:24:10 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 25 Aug 2006 - 02:24:33 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.