[19382] in s-news-athena
Re: [S] Repeating a Data Frame
daemon@ATHENA.MIT.EDU (Bill Venables)
Sat Oct 2 19:45:27 1999
Message-Id: <199910022335.JAA21989@snowy.nsw.cmis.CSIRO.AU>
To: "Joseph S. Verducci" <jsv@stat.ohio-state.edu>
Cc: "Humbolt, Allen" <HumboltA@kochind.com>, s-news@wubios.wustl.edu
In-Reply-To: Your message of "Sat, 02 Oct 1999 11:24:28 -0400."
<Pine.SOL.4.10.9910021122030.3432-100000@jove.mps.ohio-state.edu>
Date: Sun, 03 Oct 1999 09:33:17 +1000
From: Bill Venables <William.Venables@cmis.CSIRO.AU>
>
> How about a variant of this problem, where we want
> to repeat the i th row of Dat nreps[i] times?
Charles Berry pointed out the obvious efficient solution that
solves this as well as the previous problem - use an index
vector! The idea can be made into a one-line function:
rep.matrix <-
function(matrix, ...) matrix[rep(seq(1:nrow(matrix)), ...), ]
The `matrix' argument can be a genuine matrix or any matrix-like
structure such as a data frame. The ... argument can be a times=
or a length.out= argument, as for rep().
So the original problem would be solved using
BigDat <- rep.matrix(Dat, times = 27)
or
BigDat <- rep.matrix(Dat, length.out = 27*nrow(Dat))
(if you want to be a little obscure) and the new problem would be
handled by
BigDat <- rep.matrix(Dat, times = nreps)
[Hmm. Now why didn't I think of that? :-)]
This same idea is handy if ever you need to bootstrap a data
frame, of course:
Dat.boot <- Dat[sample(1:nrow(Dat), nrow(Dat), rep = T), ]
but if you were bootstrapping a fitted model it would be better
to use sample() to generate a subset vector and update on it
rather than make a duplicate copy of the data frame.
Bill Venables.
--
-----------------------------------------------------------------
Bill Venables, Statistician, CMIS Environmetrics Project.
Physical address: Postal address:
CSIRO Marine Laboratories, PO Box 120,
233 Middle St, Cleveland, Queensland Cleveland, Qld, 4163
AUSTRALIA AUSTRALIA
Telephone: +61 7 3826 7251 Email: Bill.Venables@cmis.csiro.au
Fax: +61 7 3826 7304
-----------------------------------------------------------------------
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