[R] apply to a matrix and insert in the middle of an array

Maas James Dr (MED) J.Maas at uea.ac.uk
Sun Oct 9 19:04:42 CEST 2011


If possible I'd like to produce a function that applies a formula to a column in a matrix (essentially calculating the mse) and then inserts it between values of a an array ...

confusing I know, here is an example of what I'm trying to accomplish:

## create a matrix
(a <- matrix(c(3,6,4,8,5,9,12,15),nrow=4))
## get the mean of all columns
(b <- apply(a,2,mean))
## calculate the mse of column 1
(c <- (sd(a[,1])/sqrt(length(a[,1]))))
## calculate the mse of column 2
(d <- (sd(a[,2])/sqrt(length(a[,2]))))
## now create a new vector with each mean value and its corresponding
## mse right beside it
(e <- c(b[1],c,b[2],d))

Would anyone have any suggestions how to accomplish this using an apply statement?

Thanks a bunch,

J
===============================
Dr. Jim Maas
University of East Anglia



More information about the R-help mailing list