Aggregate NAs in R -
i'm having trouble handling nas while calculating aggregated means. please see following code:
tab=data.frame(a=c(1:3,1:3), b=c(1,2,na,3,na,na)) tab b 1 1 1 2 2 2 3 3 na 4 1 3 5 2 na 6 3 na attach(tab) aggregate(b, by=list(a), data=tab, fun=mean, na.rm=true) group.1 x 1 1 2 2 2 2 3 3 nan
i want na instead of nan if vector has nas i.e. want output
group.1 x 1 1 2 2 2 2 3 3 na
i tried using custom function:
adjmean=function(x) {if(all(is.na(x))) na else mean(x,na.rm=true)}
however, following error:
aggregate(b, by=list(a), data=tab, fun=adjmean) error in fun(x[[1l]], ...) : unused argument (data = list(a = c(1, 2, 3, 1, 2, 3), b = c(1, 2, na, 3, na, na)))
in short, if column has nas want na output instead of nan. if has few nas, should compute mean ignoring nas.
any appreciated.
thanks
this close had, replaces mean(x, na.rm=true)
custom function either computes mean of non-na values, or supplies na itself:
r> with(tab, aggregate(b, by=list(a), fun=function(x) if (any(is.finite(z<-na.omit(x)))) mean(z) else na)) group.1 x 1 1 2 2 2 2 3 3 na r>
that 1 line, broke make fit display.
and had similar idea, altered function bit more return suitable values in cases.
Comments
Post a Comment