[R] Question on memory allocation & loop

From: Manoj <manojsw_at_gmail.com>
Date: Thu 29 Jun 2006 - 12:42:54 EST


Hello All,

      I am trying to work on writing the following piece of (pseudo) code in an optimal fashion:



# Two data frames with some data

a = data.frame(somedata)
b = data.frame(somedata)

for(i in 1:nrow(dt) {
  # Merge dates for a given date into a new data frame    c = merge(a[a$dt==dt[i],),b[b$dt == dt[i],], by=c(some column)); }


Now, my understanding is that the data frame c in the above code is malloc'ed in every count of the loop. Is that assumption correct?

Is the following attempt a better way of doing things?



a = data.frame(somedata)
b = data.frame(somedata)

# Pre-allocate data frame c

c = data.frame(for some size);

for(i in 1:nrow(dt) {
  # Merge dates for a given date into a new data frame    # and copy the result into c

  copy(c, merge(a[a$dt==dt[i],),b[b$dt == dt[i],], by=c(some column));

}


Now the question is, How can I copy the merged data into my pre-allocated data frame c ? I tried rbind/cbind but they are pretty fuzzy about having the right names and dimension hence it fails.

Any help would be greatly appreciated!

Thanks.

Manoj



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jun 29 16:54:51 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 29 Jun 2006 - 18:12:09 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.