Skip to content

How to force aggregate to exclude NA ?

7 messages · Daren Tan, Gabor Grothendieck, Michael Lawrence +1 more

#
The aggregate function does "almost" all that I need to summarize a datasets, except that I can't specify exclusion of NAs without a little bit of hassle.
A B  C  D
1  E I  1 NA
2  A C NA NA
3  D I NA  3
4  C I  2  4
5  A C  3  2
6  E J  1  2
7  D J  2  2
8  C G  4  1
9  C D NA  3
10 B G  3 NA
11 C B  4  2
12 A B NA NA
13 E A NA  4
14 B B  3  3
15 E I  4  1
16 E J  3  1
17 B J  4  4
18 B J  1  3
19 D D  4  2
20 B B  4  3
Group.1  C  D
1       A NA NA
2       B 15 NA
3       C NA 10
4       D NA  7
5       E NA NA
Group.1 C D
1       A 3 3
2       B 5 5
3       C 4 4
4       D 3 3
5       E 5 5

My own defined version of length and sum to exclude NA
Group.1  C  D
1       A  3  2
2       B 15 13
3       C 10 10
4       D  6  7
5       E  9  8
Group.1 C D
1       A 1 1
2       B 5 4
3       C 3 4
4       D 2 3
5       E 4 4

There are other statistics I need to compute e.g. var, sd, and it is a hassle to create customized versions to exclude NA. Any alternative approaches ?


 
 
_________________________________________________________________
[[elided Hotmail spam]]
#
Try

aggregate(m[, -(1:2)], m[1], sum, na.rm = TRUE)
aggregate(!is.na(m[, -(1:2)]), m[1], sum, na.rm = TRUE)

# or (this uses row names rather than a column for the group):

rowsum(m[, -(1:2)], m[,1], na.rm = TRUE)
rowsum(0+!is.na(m[, -(1:2)]), m[,1], na.rm = TRUE)
On Sun, Dec 7, 2008 at 7:06 AM, Daren Tan <daren76 at hotmail.com> wrote:
#
How about writing a function to do the customisation for you?

na.rm <- function(f) {
  function(x, ...) f(x[!is.na(x)], ...)
}

aggregate(m[,-c(1:2)], by=list(m[,1]), na.rm(sum))
aggregate(m[,-c(1:2)], by=list(m[,1]), na.rm(length))

Hadley
#
Actually the second aggregate and second rowsum don't need the na.rm = TRUE
so we only need:

aggregate(!is.na(m[, -(1:2)]), m[1], sum)
rowsum(0+!is.na(m[, -(1:2)]), m[,1])

You might also want to look at summaryBy in the doBy package.

On Sun, Dec 7, 2008 at 7:43 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
#
How to use the na.rm function outside aggregate ? I tried 
 
na.rm <- function(f) {
  function(x, ...) f(x[!is.na(x)], ...)
}
function(x, ...) f(x[!is.na(x)], ...)
Error in na.rm(sum, c(NA, 1, 2)) : unused argument(s) (c(NA, 1, 2))
#
On Sun, Dec 7, 2008 at 10:10 AM, Daren Tan <daren76 at hotmail.com> wrote:
na.rm(sum)(c(NA, 1, 2))

Hadley