[19383] in s-news-athena
Re: (not so) Quick Summary: [S] Repeating a Data Frame
daemon@ATHENA.MIT.EDU (Joseph S. Verducci)
Sat Oct 2 20:08:36 1999
Date: Sat, 2 Oct 1999 20:01:15 -0400 (EDT)
From: "Joseph S. Verducci" <jsv@stat.ohio-state.edu>
To: s-news@wubios.wustl.edu
In-Reply-To: <Pine.SOL.4.10.9910021308420.3432-100000@jove.mps.ohio-state.edu>
Message-Id: <Pine.SOL.4.10.9910021956210.3432-100000@jove.mps.ohio-state.edu>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Dear colleagues,
I inadvertantly did NOT send this to the list
(under the subject heading "Quick Summary") when I
thought I had. Now it serves to credit Tim Hesterberg with the
first reply.
_______________________________________________________
On Sat, 2 Oct 1999, Joseph S. Verducci wrote:
>
> What must be the definitive answer by virtue of beautiful
> simplicity comes from Tim Hesterberg:
>
> > I've got a dataframe I'll call "Dat" that I'd like to repeat 27 times.
> > I want to do something like
> > BigDat <- rbind(rep(Dat, 27))
>
> Try:
> Dat[ rep(1:nrow(Dat), 27), ]
>
> If you want to allow duplicate row names (faster), first do:
> attr(Dat, dup.row.names) <- T
>
> >How about a variant of this problem, where we want
> > to repeat the i th row of Dat nreps[i] times?
>
> Dat[ rep(1:nrow(Dat), nreps), ]
>
> ______________________________________________________________
>
>
> On Sat, 2 Oct 1999, Joseph S. Verducci wrote:
>
> >
> > For a matrix dat, one solution is
> >
> > repline_ function(line){
> > matrix(rep(line[2:length(line)],line[1]),nrow=line[1])}
> > reprows _ function(dat,nreps){
> > matrix(unlist(apply(cbind(nreps,dat),1,repline)),
> > nrow=sum(nreps),ncol=ncol(dat),byrow=T)}
> >
> > _____________________________________________
> >
> > On Sat, 2 Oct 1999, Joseph S. Verducci wrote:
> >
> > >
> > > How about a variant of this problem, where we want
> > > to repeat the i th row of Dat nreps[i] times?
> > >
> > > _____________________________________________
> > >
> > > On Sat, 2 Oct 1999, Bill Venables wrote:
> > >
> > > >
> > > > Hi Allen,
> > > >
> > > > How's tricks?
> > > >
> > > > > I've got a dataframe I'll call "Dat" that I'd like to repeat 27 times.
> > > > > I want to do something like
> > > > > BigDat <- rbind(rep(Dat, 27))
> > > > > but that doesn't work like I want when Dat is a dataframe.
> > > > > It just creates the same number of rows with my columns repeated 27 times.
> > > > > I want BigDat to have the same number of columns with 27 times as many rows.
> > > > >
> > > > > Can someone show me how to do this?
> > > >
> > > > I think it needs a loop. The simplest is certainly
> > > >
> > > > BigDat <- NULL
> > > > for(i in 1:27) BigDat <- rbind(BigDat, Dat)
> > > >
> > > > If 27 really is the number and you want to do this sort of thing
> > > > all the time you can slick things up just slightly by
> > > >
> > > > BigDat <- Dat
> > > > for(i in 1:3) BigDat <- rbind(BigDat, BigDat, BigDat)
> > > >
> > > > [think about it...]
> > > >
> > > > You can hide the loop in various clever ways, such as
> > > >
> > > > BigDat <- do.call("rbind", lapply(1:27, function(x) Dat))
> > > >
> > > > but I'd be very surprised if you gained all that much...
> > > >
> > > > Bill.
> > > > --
> > > > -----------------------------------------------------------------
> > > > 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
> > > >
> > >
> > > -----------------------------------------------------------------------
> > > 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
> > >
> >
> >
>
>
-----------------------------------------------------------------------
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