[R] Neat way of using R for pivoting?

BANNISTER, Keith keith.bannister at astrium.eads.net
Tue Sep 20 17:46:01 CEST 2005


I'd like to use R to do what excel pivot tables do, and plot results.

I've never used R before, and I've managed to do something, but it's quite a
lot of code to do something simple. I can't help but think I'm not "Doing it
the R way".

I could be using R for the wrong thing, in which case, please tell me off.

I was hoping something like plot(by(t, factor(t$snr), summary)) would do
something, but it doesn't.

Say my data is (for example)
SNR	timeError
4	1.3
4	2.1
4	1.2
6	2.1
6	2.2
6	2.1
8	3.2
8	3.7
8	3.1

I want to produce a plot of SNR vs mean(timeError) with error bars of
magnitude 3 sigma.

here's what I've got so far (without the error bars. I can't do that yet).

I'm sure it's the wrong way to go about this:

******* BEGIN SNIPPET *******

get_stats <- function(t) {
cnfac <- factor(t$cnset);
mu <- as.list(by(t$snr, cnfac, mean));
tvar <- as.list(by(t$snr, cnfac, var));
t <- list(mu=mu, var=tvar);

vn <- read.table('vn.csv', sep=',');
vn_stats <- get_stats(vn);

vsn <- read.table('vsn.csv', sep=',');
vsn_stats <- get_stats(vsn);

snrs <- as.numeric(names(vn_stats$mu))

matplot(snrs, cbind(vn_stats$mu, vsn_stats$mu));

matplot(snrs, cbind(vn_stats$var, vsn_stats$var));

******* END SNIPPET *******

Appreciate any helpful hints from the pros.


p.s. We've been having rather a good time around the office recently with
"International Talk Like a Pirate Day" (www.yarr.org.uk). R fits in very
well: "I be usin' Arrrgghhhh for my post processin'".

Keith Bannister

Electrical Engineer
Astrium Ltd

This email is for the intended addressee only.
If you have received it in error then you must not use, retain, disseminate or otherwise deal with it.
Please notify the sender by return email.
The views of the author may not necessarily constitute the views of EADS Astrium Limited.
Nothing in this email shall bind EADS Astrium Limited in any contract or obligation.

EADS Astrium Limited, Registered in England and Wales No. 2449259
Registered Office: Gunnels Wood Road, Stevenage, Hertfordshire, SG1 2AS, England

More information about the R-help mailing list