Hi, is there a way I can calculate a summary statistics for a columns matrix let say we have this matrix x <- matrix(sample(1:8000),nrow=100) colnames(x)<- paste("Col",1:ncol(x),sep="") if I used summary summary(x) i get the output for each column but I need the output to be in matrix with rownames and all the columns beside it this how I want it Col76 Col77 Min. : 7 39 1st Qu. : 1846 1630 Median : 3631 3376 Mean : 3804 3617 Sd : 3rd Qu.: 5772 5544 IQR : Max. : 7952 7779 Is there an easy way? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489.html Sent from the R help mailing list archive at Nabble.com.
frespider wrote> Hi, > > is there a way I can calculate a summary statistics for a columns matrix > let say we have this matrix > x <- matrix(sample(1:8000),nrow=100) > colnames(x)<- paste("Col",1:ncol(x),sep="") > > if I used summary > summary(x) > > i get the output for each column but I need the output to be in matrix > with rownames and all the columns beside it > > this how I want it > > Col76 Col77 > Min. : 7 39 > 1st Qu. : 1846 1630 > Median : 3631 3376 > Mean : 3804 3617 > Sd : > 3rd Qu.: 5772 5544 > IQR : > Max. : 7952 7779 > > Is there an easy way? > > ThanksHow about ... x <- matrix(sample(1:8000),nrow=100) colnames(x)<- paste("Col",1:ncol(x),sep="") apply(x,2,function(x) c(summary(x), sd=sd(x), IQR=IQR(x))) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650490.html Sent from the R help mailing list archive at Nabble.com.
There is still missing some statistics, like sd and IQR and I prefer the output to be matrix Thanks Date: Thu, 22 Nov 2012 18:00:20 -0800 From: ml-node+s789695n4650493h34@n4.nabble.com To: frespider@hotmail.com Subject: Re: Summary statistics for matrix columns HI, You could try this: set.seed(125) x <- matrix(sample(1:80),nrow=8) colnames(x)<- paste("Col",1:ncol(x),sep="") sapply(data.frame(x),function(x) summary(x)) # Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 Col10 #Min. 10.00 1.00 17.00 3.00 18.00 11.00 13.00 15.00 2.00 6.00 #1st Qu. 24.75 29.50 26.00 7.75 40.00 17.25 27.50 34.75 24.50 12.50 #Median 34.00 46.00 42.50 35.50 49.50 23.50 51.50 51.50 33.50 48.00 #Mean 42.50 42.75 41.75 35.75 44.88 26.88 44.75 50.12 34.88 40.75 #3rd Qu. 67.75 58.50 50.00 63.25 54.25 30.25 56.25 70.50 45.25 63.00 #Max. 74.00 77.00 76.00 70.00 65.00 63.00 79.00 80.00 71.00 72.00 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650493.html To unsubscribe from Summary statistics for matrix columns, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650494.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
HI, but Sd and IQR not in the order I want , Thanks Date: Thu, 22 Nov 2012 18:08:57 -0800 From: ml-node+s789695n4650496h27@n4.nabble.com To: frespider@hotmail.com Subject: RE: Summary statistics for matrix columns Hi, How about this: res<-do.call(cbind,lapply(split(x,col(x)),function(x) c(summary(x),sd=sd(x),IQR=IQR(x)))) colnames(res)<-colnames(x) is.matrix(res) [1] TRUE res Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Min. 10.00000 1.00000 17.00000 3.00000 18.00000 11.00000 13.00000 15.00000 1st Qu. 24.75000 29.50000 26.00000 7.75000 40.00000 17.25000 27.50000 34.75000 Median 34.00000 46.00000 42.50000 35.50000 49.50000 23.50000 51.50000 51.50000 Mean 42.50000 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 50.12000 3rd Qu. 67.75000 58.50000 50.00000 63.25000 54.25000 30.25000 56.25000 70.50000 Max. 74.00000 77.00000 76.00000 70.00000 65.00000 63.00000 79.00000 80.00000 sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 IQR 43.00000 29.00000 24.00000 55.50000 14.25000 13.00000 28.75000 35.75000 Col9 Col10 Min. 2.00000 6.00000 1st Qu. 24.50000 12.50000 Median 33.50000 48.00000 Mean 34.88000 40.75000 3rd Qu. 45.25000 63.00000 Max. 71.00000 72.00000 sd 24.39811 28.21727 IQR 20.75000 50.50000 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650496.html To unsubscribe from Summary statistics for matrix columns, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650497.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
I also don't like to use split function because I have like around 800 columns Date: Thu, 22 Nov 2012 18:08:54 -0800 From: ml-node+s789695n4650496h27@n4.nabble.com To: frespider@hotmail.com Subject: RE: Summary statistics for matrix columns Hi, How about this: res<-do.call(cbind,lapply(split(x,col(x)),function(x) c(summary(x),sd=sd(x),IQR=IQR(x)))) colnames(res)<-colnames(x) is.matrix(res) [1] TRUE res Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Min. 10.00000 1.00000 17.00000 3.00000 18.00000 11.00000 13.00000 15.00000 1st Qu. 24.75000 29.50000 26.00000 7.75000 40.00000 17.25000 27.50000 34.75000 Median 34.00000 46.00000 42.50000 35.50000 49.50000 23.50000 51.50000 51.50000 Mean 42.50000 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 50.12000 3rd Qu. 67.75000 58.50000 50.00000 63.25000 54.25000 30.25000 56.25000 70.50000 Max. 74.00000 77.00000 76.00000 70.00000 65.00000 63.00000 79.00000 80.00000 sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 IQR 43.00000 29.00000 24.00000 55.50000 14.25000 13.00000 28.75000 35.75000 Col9 Col10 Min. 2.00000 6.00000 1st Qu. 24.50000 12.50000 Median 33.50000 48.00000 Mean 34.88000 40.75000 3rd Qu. 45.25000 63.00000 Max. 71.00000 72.00000 sd 24.39811 28.21727 IQR 20.75000 50.50000 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650496.html This email was sent by arun kirshna (via Nabble) -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650498.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Hi, it is possible. but don't you think it will slow the code if you convert to data.frame? Thanks Date: Thu, 22 Nov 2012 18:31:35 -0800 From: ml-node+s789695n4650500h51@n4.nabble.com To: frespider@hotmail.com Subject: RE: Summary statistics for matrix columns HI, Is it possible to use as.matrix()? res<-sapply(data.frame(x),function(x) c(summary(x),sd=sd(x),IQR=IQR(x))) res1<-as.matrix(res) is.matrix(res1) #[1] TRUE res1[c(1:4,7,5,8,6),] # Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 #Min. 10.00000 1.00000 17.00000 3.00000 18.00000 11.00000 13.00000 15.00000 #1st Qu. 24.75000 29.50000 26.00000 7.75000 40.00000 17.25000 27.50000 34.75000 #Median 34.00000 46.00000 42.50000 35.50000 49.50000 23.50000 51.50000 51.50000 #Mean 42.50000 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 50.12000 #sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 #3rd Qu. 67.75000 58.50000 50.00000 63.25000 54.25000 30.25000 56.25000 70.50000 #IQR 43.00000 29.00000 24.00000 55.50000 14.25000 13.00000 28.75000 35.75000 #Max. 74.00000 77.00000 76.00000 70.00000 65.00000 63.00000 79.00000 80.00000 # Col9 Col10 #Min. 2.00000 6.00000 #1st Qu. 24.50000 12.50000 #Median 33.50000 48.00000 #Mean 34.88000 40.75000 #sd 24.39811 28.21727 #3rd Qu. 45.25000 63.00000 #IQR 20.75000 50.50000 #Max. 71.00000 72.00000 Solves the order and the matrix output! A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650500.html To unsubscribe from Summary statistics for matrix columns, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650501.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
frespider wrote> Hi, > > it is possible. but don't you think it will slow the code if you convert > to data.frame? > > Thanks > > Date: Thu, 22 Nov 2012 18:31:35 -0800 > From:> ml-node+s789695n4650500h51 at .nabble> To:> frespider@> Subject: RE: Summary statistics for matrix columns > > > > HI, > > Is it possible to use as.matrix()? > > res<-sapply(data.frame(x),function(x) c(summary(x),sd=sd(x),IQR=IQR(x))) > > res1<-as.matrix(res) > > is.matrix(res1) > > #[1] TRUE > > res1[c(1:4,7,5,8,6),] > > # Col1 Col2 Col3 Col4 Col5 Col6 Col7 > Col8 > > #Min. 10.00000 1.00000 17.00000 3.00000 18.00000 11.00000 13.00000 > 15.00000 > > #1st Qu. 24.75000 29.50000 26.00000 7.75000 40.00000 17.25000 27.50000 > 34.75000 > > #Median 34.00000 46.00000 42.50000 35.50000 49.50000 23.50000 51.50000 > 51.50000 > > #Mean 42.50000 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 > 50.12000 > > #sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 > 25.51995 > > #3rd Qu. 67.75000 58.50000 50.00000 63.25000 54.25000 30.25000 56.25000 > 70.50000 > > #IQR 43.00000 29.00000 24.00000 55.50000 14.25000 13.00000 28.75000 > 35.75000 > > #Max. 74.00000 77.00000 76.00000 70.00000 65.00000 63.00000 79.00000 > 80.00000 > > # Col9 Col10 > > #Min. 2.00000 6.00000 > > #1st Qu. 24.50000 12.50000 > > #Median 33.50000 48.00000 > > #Mean 34.88000 40.75000 > > #sd 24.39811 28.21727 > > #3rd Qu. 45.25000 63.00000 > > #IQR 20.75000 50.50000 > > #Max. 71.00000 72.00000 > > Solves the order and the matrix output! > > A.K. > > > > > > > > > > > > > > > If you reply to this email, your message will be added to the discussion > below: > > http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650500.html > > > > To unsubscribe from Summary statistics for matrix columns, click here. > > NAMLThen 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.