[R] Problem with by(... , median)

Michal Figurski figurski at mail.med.upenn.edu
Tue Jun 10 15:18:22 CEST 2008

Hello everyone,

I am new to R, I have been using SAS for a while. Not surprisingly, I 
find R much better in graphics, which is publication ready right away.

Recently, I have been trying to calculate some basic statistics using R. 
I have a dataset of multiple rows per subject. For example:

Subject	Date	Factor1	Factor2	Factor3
P1	0.5	1	1	3
P1	1	3	2	5
P1	2	3	5	NA
P2	0.5	1	6	4
P2	1	2	NA	7
P2	2	3	1	2

I wanted to get the 'by subject' statistics of the factors. I used the 
following code: a=by(dat1, dat1$Subject, mean, na.rm=TRUE) - this is for 
a mean.
However, when I try that with a 'median', I get an error message. It is 
strange to me, because with the 'summary' option, I get the median 
values for all factors separately, among other information. What I need 
is just a median, because I later need it for plotting.
I have read old posts on the archive, and I agree that the action of 
'median' is according to the documentation. However, I think this is a 
serious inconvenience, and it should be changed to work in the same way 
as mean, summary or sd options.

Nevertheless, accepting the reality, I have a question to all: how to 
obtain the by-subject medians of factors in such dataset?

Thanks in advance,

Michal J. Figurski
HUP, Pathology & Laboratory Medicine
Xenobiotics Toxicokinetics Research Laboratory
3400 Spruce St. 7 Maloney
Philadelphia, PA 19104

More information about the R-help mailing list