tags:

views:

81

answers:

3
a = matrix(1:25,5,5)
B = capture.output(for (X in 1:5){
    A = c(min(a[,X]),quantile(a[,X],0.25),median(a[,X]),quantile(a[,X],0.75),max(a[,X]),mean(a[,X]),sd(a[,X])/m^(1/2),var(a[,X]))
    cat(A,"\n")
})

matrix(B,8,5)

What I was trying to do is to generate a table which each column has those element in A and in that order. I try to use the matrix, but seems like it doesn't really work here. Can anyone help?

               |  1  |  2  |  3  |  4  |  5  |
|---------------------------------------------
| min          |     |     |     |     |     |
|---------------------------------------------
| 1st quartile |     |     |     |     |     |
|---------------------------------------------
| median       |     |     |     |     |     |
|---------------------------------------------
| SEM          |     |     |     |     |     |
|---------------------------------------------
| VAR          |     |     |     |     |     |

The above is what I want the table to look like.

+1  A: 

You really don't want to use capture.output for capturing numeric output like that. You can create a matrix in which to store the output, or use apply as follows:

 apply(a, 2, function(x)c(min(x), quantile(x, 0.25), mean(x), sd(x)/sqrt(length(x)))) 

(I did not add all the statistics you wanted.) This is more in line with the R way of programming.

Aniko
A: 

Or, alternatively

a <- data.frame(matrix(1:25,5,5))
fn <- function (x)
    c(summary(x), Sd = sd(x), Var = var(x))

library(plyr)
colwise (fn) (a)

Returns:

              X1        X2        X3        X4        X5
Min.    1.000000  6.000000 11.000000 16.000000 21.000000
1st Qu. 2.000000  7.000000 12.000000 17.000000 22.000000
Median  3.000000  8.000000 13.000000 18.000000 23.000000
Mean    3.000000  8.000000 13.000000 18.000000 23.000000
3rd Qu. 4.000000  9.000000 14.000000 19.000000 24.000000
Max.    5.000000 10.000000 15.000000 20.000000 25.000000
Sd      1.581139  1.581139  1.581139  1.581139  1.581139
Var     2.500000  2.500000  2.500000  2.500000  2.500000
learnr
A: 

Or using plyr:

> a<=data.frame(value=1:25,set=factor(rep(1:5,each=5)))
> summaryfn<=function(v)data.frame(
+   q0=min(v),q25=quantile(v,0.25),q50=median(v),
+   q75=quantile(v,0.75),q100=max(v),mean=mean(v),
+   sd=sd(v)^(1/2),var=var(v))
> g<-ddply(a,.(set),function(x)summaryfn(x$value))
> g
  set q0 q25 q50 q75 q100 mean       sd var
1   1  1   2   3   4    5    3 1.257433 2.5
2   2  6   7   8   9   10    8 1.257433 2.5
3   3 11  12  13  14   15   13 1.257433 2.5
4   4 16  17  18  19   20   18 1.257433 2.5
5   5 21  22  23  24   25   23 1.257433 2.5
> t(g[-1])
         [,1]      [,2]      [,3]      [,4]      [,5]
q0   1.000000  6.000000 11.000000 16.000000 21.000000
q25  2.000000  7.000000 12.000000 17.000000 22.000000
q50  3.000000  8.000000 13.000000 18.000000 23.000000
q75  4.000000  9.000000 14.000000 19.000000 24.000000
q100 5.000000 10.000000 15.000000 20.000000 25.000000
mean 3.000000  8.000000 13.000000 18.000000 23.000000
sd   1.257433  1.257433  1.257433  1.257433  1.257433
var  2.500000  2.500000  2.500000  2.500000  2.500000

-Alex

Alex Brown
I don't know what `/m` did in your code, so I removed it. hence the `sd` result is different.
Alex Brown