Hi, Is there a way to build up a vector, item by item. In perl, we can "push" an item onto an array. How can we can do this in R? I have a loop that generates values as it goes. I want to end up with a vector of all the loop results. In perl it woud be: for(item in list){ result <- 2*item^2 (Or whatever formula, this is just a pseudo example) Push(@result_list, result) (This is the step I can't do in R) } Thanks!
How about ?append, but R is vectorized, so why not just result_list <- 2*item^2 , or for more complicated tasks, the apply/sapply/lapply/mapply family of functions? In general, the "for" loop construct can be avoided so you don't have to think about messy indexing. What exactly are you trying to do? -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Noah Silverman Sent: Wednesday, August 26, 2009 2:20 PM To: r help Subject: [R] Managing output Hi, Is there a way to build up a vector, item by item. In perl, we can "push" an item onto an array. How can we can do this in R? I have a loop that generates values as it goes. I want to end up with a vector of all the loop results. In perl it woud be: for(item in list){ result <- 2*item^2 (Or whatever formula, this is just a pseudo example) Push(@result_list, result) (This is the step I can't do in R) } Thanks! ______________________________________________ R-help at 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.
On 26/08/2009 3:20 PM, Noah Silverman wrote:> Hi, > > > Is there a way to build up a vector, item by item. In perl, we can > "push" an item onto an array. How can we can do this in R? > I have a loop that generates values as it goes. I want to end up with a > vector of all the loop results. > > In perl it woud be: > > for(item in list){ > result <- 2*item^2 (Or whatever formula, this is just a pseudo example) > Push(@result_list, result) (This is the step I can't do in R) > } > > > Thanks!Use the c() function, e.g. result <- numeric(0) for (item in list) { result <- 2*item^2 result_list <- c(result_list, result) } If the list is long, this is an extremely inefficient style of R programming, but for a short list it should be fine. Duncan Murdoch
Noah Silverman <noah <at> smartmediacorp.com> writes:> > Hi, > > Is there a way to build up a vector, item by item. In perl, we can > "push" an item onto an array. How can we can do this in R? > I have a loop that generates values as it goes. I want to end up with a > vector of all the loop results. > > In perl it woud be: > > for(item in list){ > result <- 2*item^2 (Or whatever formula, this is just a pseudo example) > Push(@result_list, result) (This is the step I can't do in R) > } >Just do add an item at the end. Example:> mydat <- 1 > mydat[2] <- 33 > mydat[3] <- -4 > mydat[4] <- 12 > mydat[length(mydat)+1] <- 22 > mydat[1] 1 33 -4 12 22> mydat[length(mydat)+1] <- 5623 > mydat[1] 1 33 -4 12 22 5623> mydat[length(mydat)+1] <- 0.8962 > mydat[1] 1.0000 33.0000 -4.0000 12.0000 22.0000 5623.0000 0.8962 When doing it in a loop, don't use length() over and over, just count up the index. For many entries, it would be faster, if you just allocate the array that should be written, by just writing 0 into it, or empty strings or something like this...> mydat[1:100] <- 0 > mydat[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0> mydat <- 1 > mydat[2] <- 33 > mydat[3] <- -4 > mydat[1] 1 33 -4 instead of "mydat[1:100] <- 0" use the length of the itemlist as second parameter. Ciao, Oliver
Oliver Bandel <oliver <at> first.in-berlin.de> writes: [...]> For many entries, it would be faster, if you just allocate > the array that should be written, by just writing 0 into it, > or empty strings or something like this... > > mydat[1:100] <- 0 > > mydat > [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0[...] Maybe better combine the pre-allocation with one of the apply-functions... This would be even faster. Ciao, Oliver
Phil, Pre-allocation makes sense. However, I don't know the size of my resulting vector when starting. In my loop, I only pull off results that meet a certain threshold. -N On 8/26/09 2:07 PM, Phil Spector wrote:> Noah - > I would strongly advise you to preallocate the result vector > using numeric() or rep(), and then enter the values based on > subscripts. Allowing objects to grow inside of loops is one of > the biggest mistakes an R programmer can make. > > - Phil Spector > Statistical Computing Facility > Department of Statistics > UC Berkeley > spector@stat.berkeley.edu > > > On Wed, 26 Aug 2009, Noah Silverman wrote: > >> The actually process is REALLY complicate, I just gave a simple example >> for the list. >> >> I have a lot of steps to process the data before I get a final >> "score". (nested loops, conditional statements, etc.) >> >> Right now, I'm just printing the scores to the screen. I'd like to >> accumulate them in some kind of data structure so I can either write >> them to disk or graph them. >> >> -N >> >> On 8/26/09 12:27 PM, Erik Iverson wrote: >>> How about ?append, but R is vectorized, so why not just >>> >>> result_list<- 2*item^2 , or for more complicated tasks, the >>> apply/sapply/lapply/mapply family of functions? >>> >>> In general, the "for" loop construct can be avoided so you don't >>> have to think about messy indexing. What exactly are you trying to do? >>> >>> -----Original Message----- >>> From: r-help-bounces@r-project.org >>> [mailto:r-help-bounces@r-project.org] On Behalf Of Noah Silverman >>> Sent: Wednesday, August 26, 2009 2:20 PM >>> To: r help >>> Subject: [R] Managing output >>> >>> Hi, >>> >>> >>> Is there a way to build up a vector, item by item. In perl, we can >>> "push" an item onto an array. How can we can do this in R? >>> I have a loop that generates values as it goes. I want to end up >>> with a >>> vector of all the loop results. >>> >>> In perl it woud be: >>> >>> for(item in list){ >>> result<- 2*item^2 (Or whatever formula, this is just a pseudo >>> example) >>> Push(@result_list, result) (This is the step I can't do in R) >>> } >>> >>> >>> Thanks! >>> >>> ______________________________________________ >>> R-help@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. >>> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@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. >>[[alternative HTML version deleted]]
See minor comment below -Don At 8:07 PM +0000 8/26/09, Oliver Bandel wrote: <- first part of email omitted ->>< >For many entries, it would be faster, if you just allocate >the array that should be written, by just writing 0 into it, >or empty strings or something like this... >> mydat[1:100] <- 0Simpler is mydat <- numeric(100)> > mydat > [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >0 0 0 0 0 0 > [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >0 0 0 0 0 0 > [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> mydat <- 1 >> mydat[2] <- 33 >> mydat[3] <- -4 >> mydat >[1] 1 33 -4 > > >instead of "mydat[1:100] <- 0" >use the length of the itemlist as second parameter. > >Ciao, > Oliver > >______________________________________________ >R-help at 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062