[R] lapply drops colnames

Tony Plate tplate at blackmesacapital.com
Tue Aug 3 00:54:09 CEST 2004


If you were preferring to use lapply() rather than for() for reasons of 
efficiency,you might want to test whether there actually is any 
difference.  In a little test case, involving a data frame with 10,000 
columns, I see no big difference.  The advantage of a for loop in your 
situation is that it makes it easy to get at the column names.

 > x <- data.frame(sapply(1:10000, FUN=rnorm, n=100))
 > system.time(x1 <- unlist(lapply(x, sum)))
[1] 0.31 0.01 0.33   NA   NA
 > system.time({x2 <- numeric(ncol(x)); for (i in seq(len=ncol(x))) x2[i] 
<- sum(x[[i]])})
[1] 0.27 0.00 0.27   NA   NA
 > all.equal(x1, x2)
[1] TRUE
 >

hope this helps,

Tony Plate

At Monday 04:35 PM 8/2/2004, Jack Tanner wrote:
>Wolski wrote:
>>What you can do is to extend the column (list) by an addtional 
>>attribute  attr(mydataframe[i],"info")<-names(mydataframe)[i] and store 
>>theyr names in it.
>
>OK, that's brilliant. Any ideas on how to do this automatically for every 
>column in my dataframe? lapply(dataframe... fails for the obvious reason. 
>Should I do something like this, or is for() to be avoided even in this case?
>
> > for(i in 1:length(a)) {print(names(a)[i])}
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list