[R] Conditional Loop For Data Frame Columns
Rui Barradas
ruipbarradas at sapo.pt
Tue Jan 10 03:33:39 CET 2012
P.S.
If you want to use your function, revised, it may be a good idea: it's
faster
#write indexed mean function
meanfun <- function(x, inx, na.rm=FALSE) {
meanSD <- matrix(0, nrow=length(levels(inx)), ncol=length(3:ncol(x)))
for(i in 3:ncol(x)) {
meanSD[, i - 2] <- tapply(x[,i], ID, FUN=mean, na.rm=na.rm)}
return(meanSD)
}
# apply function to data
meanfun(SD, ID, T)
# compare results
meanfun(SD, ID, T)[, (nc-9):(nc-3)]
aggregate(SD[, 3:nc], by=list(ID), mean, na.rm=TRUE)[, (nc-8):(nc-2)]
# now make it bigger for timming
SD <- rbind(SD, SD, SD, SD, SD, SD, SD, SD)
SD <- rbind(SD, SD, SD, SD, SD, SD, SD, SD)
SD <- rbind(SD, SD, SD, SD, SD, SD, SD, SD)
SD <- rbind(SD, SD, SD, SD, SD, SD, SD, SD)
dim(SD)
ID <- SD[ ,2]
system.time(aggregate(SD[, 3:nc], by=list(ID), mean))
user system elapsed
9.72 0.01 9.75
system.time(meanfun(SD, ID))
user system elapsed
3.21 0.03 3.24
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/Conditional-Loop-For-Data-Frame-Columns-tp4276821p4280873.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list