[R] Loops and dataframes

Firas Swidan firas at cs.technion.ac.il
Fri Feb 25 12:06:22 CET 2005


Hi,
I am experiencing a long delay when using dataframes inside loops and was
wordering if this is a bug or not.
Example code:

> st <- rep(1,100000)
> ed <- rep(2,100000)
> for(i in 1:length(st)) st[i] <- ed[i] # works fine
> df <- data.frame(start=st,end=ed)
> for(i in 1:dim(df)[1]) df[i,1] <- df[i,2] #takes for ever

R: R 2.0.0 (2004-10-04)
OS: Linux, Fedora Core 2
kernel: 2.6.10-1.14_FC2
cpu: AMD Athlon XP 1600.
mem: 500MB.

The example above is only to illustrate the problem. I need loops to apply
some functions on pairs (not necessarily successive) of rows in a
dataframe.

Thankful for any advices,
Firas.




More information about the R-help mailing list