[R] Tabulation and missing values

Gabor Grothendieck ggrothendieck at gmail.com
Wed Oct 4 18:18:36 CEST 2006


Is the idea here to have the NA entries be a factor level?  Try this:

table(format(EthnicCode))

with appropriate mods if you want to rearrange the levels.


On 10/4/06, David Scott <d.scott at auckland.ac.nz> wrote:
>
> I think this is one for Gabor. I don't seem to be able to find my way to
> an answer despite numerous rereadings of factor and table.
>
> Here is a toy example:
>
>
> ### Some data
> EthnicCode <- c("European/Other", NA, "European/Other", "European/Other",
>                 "Pacific", "European/Other", "European/Other",
>                 "European/Other", "Maori", "Maori", "European/Other",
>                 "European/Other", "Asian", "Pacific")
> ### I don't want the categories in the default order
> ### I also want to be able to include or exclude NA
> ### argument exclude controls inclusion of NA
> table(EthnicCode)
> table(EthnicCode,exclude=NULL)
> ### Creating a factor allows reordering
> EthnicFactor <- factor(EthnicCode, exclude="")
> levels(EthnicFactor) <- list("Maori"="Maori","Pacific"="Pacific",
>                                 "Asian"="Asian",
>                                 "Europ/Other"="European/Other",
>                                     is.na="NA")
> ### I can tabulate with categories in the desired order
> table(EthnicFactor[,drop=TRUE])
> ### But I can't seem to include the missing observations
> table(EthnicFactor,exclude=NULL)
>
> David Scott
> _________________________________________________________________
> David Scott     Visiting (Until January 07)
>                Department of Probability and Statistics
>                The University of Sheffield
>                The Hicks Building
>                Hounsfield Road
>                Sheffield S3 7RH
>                United Kingdom
> Phone:  +44 114 222 3908
> Email:  d.scott at auckland.ac.nz
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list