[R] Question on memory allocation & loop
Manoj
manojsw at gmail.com
Thu Jun 29 04:42:54 CEST 2006
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
More information about the R-help
mailing list