While you can build up a vector like this in a for loop, this is exactly
the sort of construction that leads to excessive memory growth because on
each iteration of the loop R creates a new copy of the vector x - old
copies have no references to them, but are not deallocated until the next
automatic garbage collection. If the vector is small, this is probably of
no consequence. But if it is large, this sort of construction leads to alot
of garbage collection, slowing down the calculation dramatically (as shown
in Albyn's example).

To see the magnitude of the extra memory, and how often the garbage
collection is kicking in - look at this

gc() # to get down to a base level of memory before starting the loop

x = 5
mem=numeric(9999)
mem[1] = memory.size()
for (i in 2:10000) {
x[i] = x[i-1]+5
mem[i] = memory.size()
}
plot(mem)

notice that the peak memory allocated during the loop is > 4 MB, even
though after the loop is finished

object.size(x)
80024 bytes

Albyn's solution of allocating more than necessary and then tossing the
rest away would be much more efficient, even if you allocated 10 - 50x more
than necessary! You could also put in a test for approaching the end of the
allocated space, and have it double the length. This is more efficient
because it restricts the number of duplications of the object x.
On Tue, Apr 10, 2012 at 10:58 AM, Jessica Streicher <
j.streic...@micromata.de> wrote:

> > x<-numeric(1)
> > x
> [1] 0
> > x[2]<-2
> > x
> [1] 0 2
>
> you don't really need to define the length?
>
>
> Am 10.04.2012 um 17:45 schrieb Albyn Jones:
>
> > Here are a couple of constructions that work.
> >
> > albyn
> > ===========================================
> >
> > num <- rep(0,10)
> > for (i in 2:10)   {
> >      num[i] <- num[i-1] + 5
> >      if(num[i] > 20) break
> > }
> >> num
> > [1]  0  5 10 15 20 25  0  0  0  0
> >
> > or
> >
> > num <- rep(0,10)
> > done <- FALSE
> > i <- 2
> > while(!done){
> >     num[i] <- num[i-1] + 5
> >     if(num[i] > 20) done <- TRUE
> >     i <- i + 1
> > }
> >> num
> > [1]  0  5 10 15 20 25  0  0  0  0
> >
> >
> > On Tue, Apr 10, 2012 at 10:48:34AM -0400, Steve Lavrenz wrote:
> >> Everyone,
> >>
> >> I'm very new to R, especially when it comes to loops and functions, so
> >> please bear with me if this is an elementary question. I cannot seem to
> >> figure out how to construct a loop which runs a function until a certain
> >> value is computed. For example, say I have the following:
> >>
> >> num = numeric (10)
> >> num [1] = 0
> >> for (i in 2:10)   {
> >>      num [i] = num [i-1] + 5
> >> }
> >>
> >> This adds 5 to the preceding spot of a vector of length 10 to get the
> value
> >> in the current spot. However, say I don't just want to run this for 10
> >> spots; rather I want to run it until a certain value (say, 100) is
> computed.
> >> How I construct my loop to do this?
> >>
> >> Thanks!
> >>
> >>
> >>      [[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.
> >>
> >
> > --
> > Albyn Jones
> > Reed College
> > jo...@reed.edu
> >
> > ______________________________________________
> > 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.
>



-- 
Drew Tyre

School of Natural Resources
University of Nebraska-Lincoln
416 Hardin Hall, East Campus
3310 Holdrege Street
Lincoln, NE 68583-0974

phone: +1 402 472 4054
fax: +1 402 472 2946
email: aty...@unl.edu
http://snr.unl.edu/tyre
http://aminpractice.blogspot.com
http://www.flickr.com/photos/atiretoo

        [[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.

Reply via email to