[R] rbind/cbind

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Aug 10 16:00:32 CEST 2011


Can't you use sapply?

sapply(seq_len(10), function(i){SomeExpression(i)})

Best regards,

Thierry

> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
> Namens David Winsemius
> Verzonden: woensdag 10 augustus 2011 15:50
> Aan: Anthony Ching Ho Ng
> CC: r-help op r-project.org
> Onderwerp: Re: [R] rbind/cbind
> 
> 
> On Aug 10, 2011, at 9:08 AM, Anthony Ching Ho Ng wrote:
> 
> > Dear list,
> >
> > I wonder if there a better way to have rbind/cbind/append to create
> > the first element (if it is empty) instead of doing the following in a
> > loop?
> >
> > for (i in 1:10) {
> >  if (i == 1) {
> >     aRow = SomeExpression(i)
> >  } else {
> >    aRow = rbind(aRow,SomeExpression(i))  } }
> >
> 
> Generally one is advised not to use rbind in this manner but rather to pre-
> allocate aRow to the size needed and then to add information by rows using "[".
> 
> For a matrix this might be:
> 
> aRow <- matrix(NA, ncol=3, nrow=10)
> 
> for (i in 1:10) {
>      aRow[1,] <- SomeExpression(i)
>   }
> 
> 
> --
> David Winsemius, MD
> West Hartford, CT
> 
> ______________________________________________
> R-help op 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.



More information about the R-help mailing list