[R] mean and sd of each serial position

Bill Simpson wsimpson at uwinnipeg.ca
Fri Oct 16 20:02:23 CEST 1998


I want to do something like this in R. If I have three vectors
> a1
[1] 1 2 3
> a2
[1] 4 5 6
> a3
[1] 9 10 7

I want to compute
1. A vector that is the mean at each serial position of a1, a2, and a3.
so in this example it would have the contents
4.667, 5.667, 5.333333

2. A vector that is the SD at each serial position of a1, a2, and a3.
so in this example it would have the contents
 4.041452, 4.041452, 2.081666

Can anyone tell me a good way to do this in R? The only thing I can come
up with is

meanvec<-function(a1,a2,a3)
{
temp<-numeric(3)
for(i in 1:3)
	temp[i]<-mean(c(a1[i],a2[i], a3[i]))
return(temp)
}

sdvec<-function(a1,a2,a3)
{
temp<-numeric(3)
for(i in 1:3)
	temp[i]<-sd(c(a1[i],a2[i], a3[i]))
return(temp)
}

If this simple-minded approach is the way to go, I wonder how to modify
these functions so I don't need to know the number of vectors ahead of
time. That is, make the same function do mean on 3 vectors, 10 vectors,
etc--just include all the vectors in the function call.

Thanks very much for any help.

Bill Simpson

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list