[R] Tabulation and missing values

David Scott d.scott at auckland.ac.nz
Wed Oct 4 18:10:56 CEST 2006


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



More information about the R-help mailing list