[R] "with" and "by" and NA:
Aldi Kraja
aldi at wustl.edu
Thu Mar 26 00:36:47 CET 2009
Hi,
I have a data.frame with many variables for which I am performing the
mean by subgroup, for a pair of variables at a time, where one of them
for each pair defines the subgroup. The subgroups in the x$cm1 are 0, 1
and 2.
x
ph1 cm1
0.2345 2
1.2222 1
2.0033 0
0.0000 2
1.0033 1
0.2345 0
1.2222 2
2.0033 0
0.0000 1
1.0033 2
> meanbygroup <- as.vector(with(x, by(x$ph1, x$cm1, mean)))
> meanbygroup
if the ph1 has no missing values the above statements work fine:
[1] 1.4137000 0.7418333 0.6150000
In the moment that I introduce in the ph1 a missing value in the ph1 as NA
x
ph1 cm1
0.2345 2
NA 1
1.2222 1
.............
the above transforms into
[1] 1.4137000 NA 0.6150000
Question: is there a way I can protect this calculations from the NA
values in the ph1 (some kind of: na.rm=T)?
TIA,
Aldi
--
More information about the R-help
mailing list