Sean O'Riordain
2010-Jan-07 12:02 UTC
[R] table() and setting useNA to be there by default?
Good morning, Is there a way to get table() to default to including NAs - as in... table(..., useNA='ifany') or table(..., useNA='always') or table(..., exclude=NULL) ? I can't see a way under table() or options() or searching the archives (probably using the wrong keyword?).> t1 <- c(1,2,3,3,3,2,NA,NA,NA,NA) > table(t1)t1 1 2 3 1 2 3 I keep forgetting to allow for NAs and I was bitten *again* this morning... ideally I'd like to be able to set a default in my profile so that table(t1) will actually do table(t1, exclude=NULL) or table(t1, useNA='ifany') I could say something like..> tab1 <- function(t, ...) { table(t, ..., useNA='ifany') } > tab1(t1)t 1 2 3 <NA> 1 2 3 4 but this names it as 't' instead of 't1' which is ugly? Any other suggestions please? Thanks in advance, Sean O'Riordain Dublin Ireland
Sean O'Riordain wrote:> Good morning, > > Is there a way to get table() to default to including NAs - as in... > table(..., useNA='ifany') or table(..., useNA='always') or table(..., > exclude=NULL) ? > > I can't see a way under table() or options() or searching the archives > (probably using the wrong keyword?). > >> t1 <- c(1,2,3,3,3,2,NA,NA,NA,NA) >> table(t1) > t1 > 1 2 3 > 1 2 3 > > I keep forgetting to allow for NAs and I was bitten *again* this > morning... ideally I'd like to be able to set a default in my profile > so that table(t1) will actually do table(t1, exclude=NULL) or > table(t1, useNA='ifany') > > I could say something like.. > >> tab1 <- function(t, ...) { table(t, ..., useNA='ifany') } >> tab1(t1) > > t > 1 2 3 <NA> > 1 2 3 4 > > but this names it as 't' instead of 't1' which is ugly?How about tab1 <- function(...) table(..., useNA='ifany') -Peter Ehlers> > Any other suggestions please? > > Thanks in advance, > Sean O'Riordain > Dublin > Ireland > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Peter Ehlers University of Calgary 403.202.3921
On 01/07/2010 11:02 PM, Sean O'Riordain wrote:> Good morning, > > Is there a way to get table() to default to including NAs - as in... > table(..., useNA='ifany') or table(..., useNA='always') or table(..., > exclude=NULL) ? > > I can't see a way under table() or options() or searching the archives > (probably using the wrong keyword?). > > >> t1<- c(1,2,3,3,3,2,NA,NA,NA,NA) >> table(t1) >> > t1 > 1 2 3 > 1 2 3 > > I keep forgetting to allow for NAs and I was bitten *again* this > morning... ideally I'd like to be able to set a default in my profile > so that table(t1) will actually do table(t1, exclude=NULL) or > table(t1, useNA='ifany') > > I could say something like.. > > >> tab1<- function(t, ...) { table(t, ..., useNA='ifany') } >> tab1(t1) >> > t > 1 2 3<NA> > 1 2 3 4 > > but this names it as 't' instead of 't1' which is ugly? > > Any other suggestions please? >Hi Sean, I have managed to get rid of the ugly "t" with this: tableNA<-function(x) { varname<-deparse(substitute(x)) assign(varname,x) tabNA<-table(get(varname),useNA="always") names(attr(tabNA,"dimnames"))<-varname return(tabNA) } Seems a long way round the paddock. Jim