Skip to content
Prev 311695 / 398502 Next

Summary statistics for matrix columns

HI,
You are right.
It is slower when compared to Pete's solution:
set.seed(125)
x <- matrix(sample(1:800000),nrow=1000)
colnames(x)<- paste("Col",1:ncol(x),sep="")

system.time({
res<-sapply(data.frame(x),function(x) c(summary(x),sd=sd(x),IQR=IQR(x)))
?res1<-as.matrix(res) 
res2<-res1[c(1:4,7,5,8,6),] })
# user? system elapsed 
#? 0.596?? 0.000?? 0.597 

system.time({
res<-apply(x,2,function(x) c(Min=min(x),
??????????????????????? "1st Qu" =quantile(x, 0.25,names=FALSE),
??????????????????????? Median = quantile(x, 0.5, names=FALSE),
??????????????????????? Mean= mean(x),
??????????????????????? Sd=sd(x),
??????????????????????? "3rd Qu" = quantile(x,0.75,names=FALSE),
??????????????????????? IQR=IQR(x),
??????????????????????? Max = max(x))) })
# user? system elapsed 
?# 0.384?? 0.000?? 0.384?


A.K.



----- Original Message -----
From: Pete Brecknock <Peter.Brecknock at bp.com>
To: r-help at r-project.org
Cc: 
Sent: Friday, November 23, 2012 8:42 AM
Subject: Re: [R] Summary statistics for matrix columns

frespider wrote
Then maybe ....

x <- matrix(sample(1:8000),nrow=100) 
colnames(x)<- paste("Col",1:ncol(x),sep="") 

apply(x,2,function(x) c(Min=min(x), 
? ? ? ? ? ? ? ? ? ? ? ? "1st Qu" =quantile(x, 0.25,names=FALSE), 
? ? ? ? ? ? ? ? ? ? ? ? Median = quantile(x, 0.5, names=FALSE),
? ? ? ? ? ? ? ? ? ? ? ? Mean= mean(x),
? ? ? ? ? ? ? ? ? ? ? ? Sd=sd(x), 
? ? ? ? ? ? ? ? ? ? ? ? "3rd Qu" = quantile(x,0.75,names=FALSE),
? ? ? ? ? ? ? ? ? ? ? ? IQR=IQR(x),
? ? ? ? ? ? ? ? ? ? ? ? Max = max(x)))

HTH

Pete



--
View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650547.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.