Note that for factors with NA in the levels, is.na(f)[2] <- TRUE and is.na(f[2])<-TRUE give different results:
> f <- factor(c("A","A",NA), levels=c(NA, "A"), exclude=NULL) > str(f) Factor w/ 2 levels NA,"A": 2 2 1 > is.na(f) [1] FALSE FALSE FALSE > is.na(f[2]) <- TRUE > str(f) Factor w/ 2 levels NA,"A": 2 1 1 > is.na(f) [1] FALSE FALSE FALSE > is.na(f)[2] <- TRUE > str(f) Factor w/ 2 levels NA,"A": 2 NA 1 > is.na(f) [1] FALSE TRUE FALSE > f[2] <- NA > str(f) Factor w/ 2 levels NA,"A": 2 1 1 > is.na(f) [1] FALSE FALSE FALSE You may find it easiest to change the NA's to strings with a different name. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of andrewH > Sent: Wednesday, October 26, 2011 9:22 PM > To: r-help@r-project.org > Subject: Re: [R] Consistant test for NAs in a factor when exclude = NULL? > > Thanks Jeff! I appreciate you sharing your experience. > > My data set is survey data, 13,209 records over nine years, collected by > someone else, converted from SPSS format. It includes missing values, > identified however SPSS does so, and translated to NAs by the import > process. It also includes values along the lines of "none of your business" > or "beats me" that are missing so far as I am concerned. I have assigned NAs > to these values. Now I am trying to figure out some things about where > these missing values are -- whether they are disproportionately located in > any period or group. I have been trying to get counts for subsets, but I > have not been able to make the subset counts add up to the total counts that > I get from, e.g. summary. > > So I wrote these simplified versions, and even for the simplest examples, I > could not find a function that correctly identified the NAs that I knew were > there because I put them there myself. That is why I am looking for help. > Does this make sense? > > Warmest regards, andrewH > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Consistant-test-for-NAs-in-a-factor-when- > exclude-NULL-tp3942755p3943157.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.