[R] S. function calculating x +- y

Uwe Ligges ligges at statistik.tu-dortmund.de
Fri Mar 4 18:34:49 CET 2011



On 04.03.2011 18:22, Sascha Vieweg wrote:
> Hello, I am looking for an elegant one-liner for the following operation:
>
> x <- rnorm(10)
> y <- runif(10)
> c(mean(x)-mean(y), mean(x)+mean(y))
>
> I thought about
>
> apply(data.frame(x, y), 2, mean)
>
> but I don't know how to apply the +- operation on the result of apply.
> Thanks, *S*
>


The most elegant way probably is the way you had above for this setting, 
otherwise you could do, e.g.:

df <- data.frame(x, y)

sapply(c("-", "+"), function(f, dat)
   do.call(get(f), as.list(colMeans(dat))), dat = df)


Uwe Ligges



More information about the R-help mailing list