Suppose for some reason I need to loop through rows in a data frame.
I create a simple data.frame
df <- data.frame(id = sample(1e6, 1e7, replace = TRUE))
It seems that f2 is much slower than f1, while I expected them to be equivalent.
f1 <- function(v){ for (obs in 1:(1e6) ){ a <- v[obs] } a } system.time(f1(df$id)) f2 <- function(){ for (obs in 1:(1e6) ){ a <- df$id[obs] } a } system.time(f2())
Do you know, why? Do they use exactly the same amount of memory?
performance r
Matthew
source share