Skip to content

Still can't find missing data - How do I get NA in xtabs with factors?

2 messages · Farley, Robert, Uwe Ligges

#
Let's see if I understand this.  Do I iterate through
    x <- factor(x, levels(c(levels(x), NA), exclude=NULL)
for each of the few hundred variables (x) in my data frame?


I tried to do this all at once and failed:
Data1 Data2  Data3 Weight
101   Sam   Red Banana    1.1
102   Sam Green Banana    2.1
103   Sam  Blue Orange    2.1
104  Fred   Red Orange    2.1
105  Fred Green  Guava    2.1
106  Fred  Blue  Guava    2.1
107  <NA>   Red   Pear   50.1
108  <NA> Green   Pear   50.1
109  <NA>  Blue   <NA> 1000.2
Error in levels(c(levels(ToyData), NA), exclude = NULL, na.action = na.pass) :
  unused argument(s) (exclude = NULL, na.action = function (object, ...)
Data1  Data2  Data3 Weight
  <NA>   <NA>   <NA>   <NA>
Levels:
But it didn't work.  Don't I need to do this separately for each variable?



Is there a way to get read.spss to insert "NA" levels for each variable when I create the data frame?  Is this because SPSS (and STATA) allow "NA" as an "undeclared level" and R does not?


Will this be a problem with read.dta as well?




Robert Farley
Metro
www.Metro.net


-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Thursday, May 28, 2009 20:39
To: Farley, Robert
Subject: RE: [R] Still can't find missing data

In R factors don't save space over character vectors - only
one copy of any given string is kept in memory in either case.
Factors do let you order the levels in the way you want and
that is often important in presentations.

You can add NA to the list of levels of a factor by doing
    x <- factor(x, levels(c(levels(x), NA), exclude=NULL)
where 'x' represents each factor in your dataset.  After
doing that is.na(x) will be all FALSE and you may not
want that for other situations.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
#
Farley, Robert wrote:
Yes, for all being factors.

Best,
Uwe Ligges