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
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. >
On Thu, 2006-10-05 at 05:10 +1300, David Scott 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")^^^^^^^^^^^ Are you sure you want that last entry. You are basically creating a level "is.na", for those entries that have character "NA" (not same as NA). When you use table(EthnicFactor[,drop=TRUE]) you are dropping empty levels, not dropping NAs which is why your self-inflicted is.na disappears, but not when you use exclude=NULL, which I don't get either, as ?table has exclude = c(NA, NaN) not NULL. Just do: table(factor(EthnicCode)) but that loses the NAs in the factor created (see ?factor and argument exclude). Compare: dat <- factor(EthnicCode) dat # preserves NAs as a level dat2 <- factor(EthnicCode, exclude = "") dat2 table(dat) table(dat2) # NAs as a level & displayed table(dat2, exclude = NA) # no NAs Is that what you were looking for? HTH G> ### 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-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC & ENSIS, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%