Never (!) use something like == NA in a logical statement. Instead always
use is.na() to check for missing values. This is a general rule with no
exception. Google it in case you are interested, == NA is error prone
because NA is a special character in the R language so it needs a special
methods associated with it.
Jens
-----Original Message-----
From: gov2001-l-bounces at
lists.fas.harvard.edu [mailto:gov2001-l-
bounces at
lists.fas.harvard.edu] On Behalf Of Colin Brown
Sent: Wednesday, March 26, 2008 9:38 AM
To: gov2001-l at
lists.fas.harvard.edu
Subject: [gov2001-l] Logical Statements
Low priority question (I've found a work-around), but I'd like to know
why I'm
having problems with this R code:
data3$epop.inequal.11[data3$ordepop == 1 & data3$ordinequality == 1] <-
1
data3$epop.inequal.11[data3$ordepop != 1 | data3$ordinequality != 1] <-
0
data3$epop.inequal.11[data3$ordepop == NA | data3$ordinequality == NA]
<- NA
What I'm trying to do with this is create a new dummy variable based on
the
values of two ordinal variables. The two that it's drawing from are
scaled 1-3
(low, medium, high), and this particular dummy variable is supposed to
be a 1
when both of the ordinal variables = 1, otherwise 0.
It's correctly re-coding the 1s and 0s, but the second line is causing
some of
the missing values to be coded as missing and others to be coded as 0s.
The following code works, but it's not as straightforward or as easy to
describe. Why is the first set of commands working mostly correctly but
not
fully?
data3$epop.inequal.11[data3$ordepop == 1 & data3$ordinequality == 1] <-
1
data3$epop.inequal.11[data3$ordinequality != 1] <- 0
data3$epop.inequal.11[data3$ordepop != 1 & data3$ordinequality == 1] <-
0
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l