[19389] in s-news-athena
[S] Summary of "Reapeating a Data Frame (rows of) 27 Times"
daemon@ATHENA.MIT.EDU (Humbolt, Allen)
Mon Oct 4 11:04:36 1999
Message-Id: <8FE5933BB8DBD111803F00805F9F74BD025B851B@ictmail10.kochind.com>
From: "Humbolt, Allen" <HumboltA@kochind.com>
To: "'s-news@wubios.wustl.edu'" <s-news@wubios.wustl.edu>
Date: Mon, 4 Oct 1999 09:55:02 -0500
Mime-Version: 1.0
Content-Type: text/plain;
charset=us-ascii
Content-Transfer-Encoding: 7bit
Thanks to all who replied. My original question was how to repeat the rows
of data frame "Dat" 27 times, with the suggestion that something like
rbind(rep(Dat, 27)) ought to work.
The most efficient and intuitively pleasing (to me) answer came from Sam
Buttrey, Bill Dunlap, Tim Hesterberg, and Joseph Verducci who suggested the
result below. As an reply to some recent s-news responses attributing the
first solution to Tim, I'll mention that Sam was actually the first to send
me a personal reply with this nice and very simple and efficient solution.
> BigDat <- Dat[rep (1:nrow(Dat), 27),]
Charles Berry also sent basically the same idea, which Bill Venables
expanded upon.
As always, I learned useful concepts from several of the answers, so I
summarize them below along with the time required to implement them on my PC
for one of my data sets. Thanks again to all.
Allen Humbolt
Koch Industries, Inc.
humbolta@kochind.com
SOLUTIONS
1. Bill Venables, Nick Locantore, Tim Hesterberg 67.70 sec.
> BigDat <- NULL
> for(i in 1:27) BigDat <- rbind(BigDat, Dat)
2. Bill Venables 15.93 sec.
> BigDat <- do.call("rbind", lapply(1:27, function(x) Dat))
3. Sam Buttrey, Bill Dunlap, Joseph Verducci 0.45 sec.
> BigDat <- Dat[rep (1:nrow(Dat), 27),]
4. Peter Perkins didn't work
> BigDat <- Dat
> BigDat <- data.frame(1:(27*nrow(Dat)),Dat)[,-1]
However, Peter's solution was efficient and took 1.03 minutes for me via
Splus 2000.
I didn't check the details of what I did carefully; but the interesting
question may be why this did not work on Splus 4.5, but did on 2000. My
project
was on Splus 4.5 so I didn't bother to try all these examples on it. I
tried it
on 2000 only because I was curious why I couldn't get it to work.
5. Erin Hodgess Wouldn't finish; but did finish for smaller
problem.
> BigDat <- data.frame(t(rep(data.frame(t(Dat)),5)))
6. Chuck Taylor 11.47 sec.
> doit <- paste("rbind(", paste(rep("Dat", 27), collapse=","), ")")
> eval(parse(text=doit))
7. Charles Berry 0.44 sec
> BigDat <- data.frame(Dat)[rep(1:318, 27)] # Dat is 318 x 27
8. Bill Venables, who attributed to Charles Berrry 0.46 sec
> rep.matrix <-
+ function(matrix, ...) matrix[rep(seq(1:nrow(matrix)), ...), ]
> BigDat <- rep.matrix(Dat, times = 27)
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news