Hi R-devel,

When using lapply upon data.frame, I notice lapply coerces data.frame to list 
before calling internal lapply function.

R> lapply
function (X, FUN, ...) 
{
    FUN <- match.fun(FUN)
    if (!is.vector(X) || is.object(X)) 
        X <- as.list(X)
    .Internal(lapply(X, FUN))
}

df <- data.frame(V1=seq(100*1024*1024), V2=rnorm(100*1024))

R> is.vector(df)            # btw, list is a vector, data.frame is a list, but 
data.frame is NOT a vector, something is not consistent ??
[1] FALSE

X <- as.list(X) is executed and takes time for large data.frame
R> object.size(df)
1258291976 bytes
R> system.time(as.list(df))
   user  system elapsed 
  1.396   0.472   1.885 

The question is: Given that data.frame is a list, is it necessary to coerce the 
data.frame to a list for lapply?

Would the following logic do the same but more efficient for lapply to run on 
data.frame?

     if (!is.vector(X) && !is.list(X) || is.object(X)) 
        X <- as.list(X)
    .Internal(lapply(X, FUN))

Thanks,

Qin
        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to