Cézar Freitas
2008-Oct-02 13:15 UTC
[R] [solutions] "tapply versus by" in function with more than 1 arguments
Thanks to all. I summarized (in order to thank the list) the solutions to help future workers searching subjects like this at R help. # Number of rows nr = 10 # Data set dataf = as.data.frame(matrix(c(rnorm(nr),rnorm(nr)*2,runif(nr),sort(c(1,1,2,2,3,3,sample(1:3,nr-6,replace=TRUE)))),ncol=4)) names(dataf)[4] = "class" #----------------------------------------------------- #Solution 1: #works, but need space to allocate the new data: splidata # Splitting your data splitdata = split(dataf,dataf$class) # Correlations correl=lapply(splitdata,function(x) cor.test(x[,1],x[,2])$estimate) res=do.call(c,correl) names(res)=paste('class',unique(dataf[,4]),sep="") res #----------------------------------------------------- #Solution 2: sapply(by(dataf[,c("V1","V2")], dataf$class, cor), '[', 3) #works, and I can specify parameters like sapply(by(dataf[,c("V1","V2")], dataf$class, cor, method="pearson"), '[', 3) #----------------------------------------------------- #Solution(s) 3: tapply(rownames(dataf), dataf$class, function(r) cor(dataf[r, "V1"],dataf[r, "V2"])) #or tapply(rownames(dataf), dataf$class, function(r) with(dataf[r, ], cor(V1, V2))) #works good; I can directly specify parameters at "function()" like tapply(rownames(dataf), dataf$class, function(r) with(dataf[r, ], cor(V1, V2, method="kendall"))) #but, in a 100 thousand rows data, it is TEN times slower than sapply (I surprised) #note that tapply(rownames(dataf), dataf$class, function(r) with(dataf[r, ], cor(V1, V2))) is SIX times slower than sapply #----------------------------------------------------- #Solution(s) 4: install.packages("plyr") library(plyr) ddply(dataf, .(class), function(df) data.frame(cor(df[, 1:2]))) daply(dataf, .(class), function(df) cor(df[, 1:2])) dlply(dataf, .(class), function(df) cor(df[, 1:2])) #Interesting library, but not good to my example - I need a vector with results #[only cor(V1,V2) and not cor(V1,V1)], that is, I don't want rectangular objects Novos endereços, o Yahoo! que você conhece. Crie um email novo com a sua cara @ymail.com ou @rocketmail.com. http://br.new.mail.yahoo.com/addresses [[alternative HTML version deleted]]