[R] Neat way of using R for pivoting?
greg.snow at ihc.com
Tue Sep 20 18:46:56 CEST 2005
>>> "BANNISTER, Keith" <keith.bannister at astrium.eads.net> 09/20/05
>> I'd like to use R to do what excel pivot tables do, and plot
R does not have pivot tables and I hope that it never does.
My experiance with pivot tables is that they encourage poor initial
by non-easily-reproducable post-hoc twiddling.
R encourages proper initial design followed by fixing the core design
where things don't turn out the way you intended.
In R I prefer to work with script files and save the file. If the
table or graph
does not turn out the way I intended, then I just edit the script file
and rerun it.
While this may be a little more work than clicking on a pivot table at
first, in the
long run I find it saves more time.
Consider the situation where you create a table/graph, then a month
boss/client/coworker finds some typos in the original data and needs
and/or graph recreated with the corrected data (or maybe a new dataset
needs a similar graph/table). With the pivot table you need to try and
everything that you clicked on and click on it again. With the R
script file you
just fix the data (or load in the new data) and rerun the script and
OK, enough of my ranting, on to helping with your problem.
>> I've never used R before, and I've managed to do something, but it's
>> lot of code to do something simple. I can't help but think I'm not
>> the R way".
>> I could be using R for the wrong thing, in which case, please tell
"by" is a bit of an overkill for this situation, tapply will probably
try this basic script as a starting place:
### start ###
my.df <- data.frame( SNR=rep( c(4,6,8), each=3),
timeError = c(1.3,2.1,1.2,2.1,2.2,2.1,3.2,3.7,3.1))
tmp.mean <- tapply( my.df$timeError, my.df$SNR, mean)
tmp.sd <- tapply( my.df$timeError, my.df$SNR, sd)
tmp.x <- unique(my.df$SNR)
plot( tmp.x, tmp.mean,
segments(tmp.x, tmp.mean-3*tmp.sd, tmp.x, tmp.mean+3*tmp.sd,
points(tmp.x, tmp.mean+3*tmp.sd, pch='-',cex=3,col='green')
points(tmp.x, tmp.mean-3*tmp.sd, pch='-',cex=3,col='green')
### end script ###
This may be even simpler with a loaded package. a quick search shows
the following functions (package in parens) that may help:
plotCI(gplots) Plot Error Bars and Confidence Intervals
errbar(Hmisc) Plot Error Bars
xYplot(Hmisc) xyplot and dotplot with Matrix Variables to
Plot Error Bars and Bands
plotCI(plotrix) Plot confidence intervals/error bars
errbar(sfsmisc) Scatter Plot with Error Bars
plotCI(sfsmisc) Plot Confidence Intervals / Error Bars
>> Appreciate any helpful hints from the pros.
hope this helps,
>> p.s. We've been having rather a good time around the office recently
>> "International Talk Like a Pirate Day" (www.yarr.org.uk). R fits in
>> well: "I be usin' Arrrgghhhh for my post processin'".
>> Keith Bannister
Greg Snow, Ph.D.
Statistical Data Center, LDS Hospital
Intermountain Health Care
greg.snow at ihc.com
More information about the R-help