Message-ID: <BLU137-W191CDF58EF4BF490BB4489B2FC0@phx.gbl>
Date: 2008-12-07T16:10:47Z
From: Daren Tan
Subject: How to force aggregate to exclude NA ?
In-Reply-To: <f8e6ff050812070545g655c2aaar7e0b9c55cf706699@mail.gmail.com>
How to use the na.rm function outside aggregate ? I tried
na.rm <- function(f) {
function(x, ...) f(x[!is.na(x)], ...)
}
>na.rm(sum(c(NA,1,2)))
function(x, ...) f(x[!is.na(x)], ...)
> na.rm(sum, c(NA,1,2))
Error in na.rm(sum, c(NA, 1, 2)) : unused argument(s) (c(NA, 1, 2))
> Date: Sun, 7 Dec 2008 07:45:14 -0600
> From: h.wickham at gmail.com
> To: daren76 at hotmail.com
> Subject: Re: [R] How to force aggregate to exclude NA ?
> CC: r-help at stat.math.ethz.ch
>
>>> aggregate(m[,-c(1:2)], by=list(m[,1]), mysum) <----------------- this computes correctly.
>> Group.1 C D
>> 1 A 3 2
>> 2 B 15 13
>> 3 C 10 10
>> 4 D 6 7
>> 5 E 9 8
>>
>>> aggregate(m[,-c(1:2)], by=list(m[,1]), mylength) <----------------- this computes correctly.
>> 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 ?
>
> 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
>
> --
> http://had.co.nz/