Skip to content

mean and sd of each serial position

6 messages · Ben Bolker, Thomas Lumley, Bill Simpson +1 more

#
I want to do something like this in R. If I have three vectors
[1] 1 2 3
[1] 4 5 6
[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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
I think "apply" is what you're looking for ...

apply(rbind(a1,a2,a3),2,mean)
apply(rbind(a1,a2,a3),2,sd)


or making it into a function:

tmpf _ function(...) {
  x _ rbind(...)
  list(mean=apply(x,2,mean),sd=apply(x,2,sd))
}


  

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Fri, 16 Oct 1998, Bill Simpson wrote:

            
Use apply():

cbind(a1,a2,a3) makes matrix

apply(cbind(a1,a2,a3),1,mean) computes the mean of each row of the matrix

In general you can do

    meanvec<-function (...) 
     	apply(cbind(...), 1, mean)

and then 
R> a1<-1:10  
R> a2<-2:11
R> a3<-3:12
R> meanvec(a1,a2,a3)
 [1]  2  3  4  5  6  7  8  9 10 11

This doesn't check that all the vectors are the same length. A more
sophisticated version is 
meanvec<-function (...) {
 if (length(unique(sapply(list(...),length)))!=1)
        stop("not all same length")
 apply(cbind(...), 1, mean)
}

R> meanvec(a1,a2,a3)     
 [1]  2  3  4  5  6  7  8  9 10 11
R> a4<-1:3
R> meanvec(a1,a2,a3,a4)
Error: not all same length


Thomas Lumley
------------------------------------------------------+------
Biostatistics		: "Never attribute to malice what  :
Uni of Washington	:  can be adequately explained by  :
Box 357232		:  incompetence" - Hanlon's Razor  :
Seattle WA 98195-7232	:				   :
------------------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Thanks very much everyone!  Yes apply() is what I needed.
Thomas, thanks very much for your function.

Bill

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
:    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

These are the "parallel mean" and "parallel standard deviation"
functions that should be part of standard R.  There is already a
"parallel maximum" function, pmax(...), for example.  In fact
there should be quite a few of these available, such as psum,
pprod, pcumsum, pdiff, ..., so if anyone has the time, feel free.

Here are two versions of pmean().  The first should be good
enough if the entire structure fits into memory; the second,
pmean.large() might be better when dealing with truly huge
vectors.  pstddev() is left as an exercise...

"pmean" <- function (..., na.rm = FALSE)  {
  args <- do.call("cbind", list(...))
  nargs <- ncol(args)
  if (na.rm) {
    OK <- !is.na(args)
    args[!OK] <- 0
    n <- as.vector(matrix(as.numeric(OK), nrow(args), nargs) %*% 
                   rep(1, nargs))
  }
  else n <- nargs
  as.vector(args %*% rep(1, nargs))/n
}

"pmean.large" <- function (..., na.rm = FALSE) {
  elts <- list(...)
  k <- max(unlist(lapply(elts, length)))
  total <- rep(as.vector(elts[[1]]), length = k)
  if (na.rm) {
    n <- !is.na(total)
    total[!n] <- 0
  }
  else {
    n <- length(elts)
  }
  for (each in elts[-1]) {
    work <- rep(as.vector(each), length = k)
    if (na.rm) {
      m <- is.na(work)
      work[m] <- 0
      n <- n + !m
    }
    total <- total + work
  }
  total/n
}
#
Thanks very much for your help, Bill.

Bill

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._