Re: [R] S. function calculating x +- y

From: Uwe Ligges <ligges_at_statistik.tu-dortmund.de>
Date: Fri, 04 Mar 2011 18:34:49 +0100

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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 04 Mar 2011 - 17:42:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Mar 2011 - 17:50:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive