Hello, I am trying the ifelse command for imputing a variable based on a conditional statement, but the NAs do not transform. The code I am trying is: ifelse (x==1, y=="NO", y=="YES"). However, the number of NAs remains the same after the attempt. I would like to turn all Y (NAs included) into a "YES" or a "NO". Thanks in advance, Ana -- Ana Genkova [[alternative HTML version deleted]]
On 02/10/2014 03:03 PM, Ana Genkova wrote:> Hello, > > I am trying the ifelse command for imputing a variable based on a > conditional statement, but the NAs do not transform. The code I am trying > is: > > ifelse (x==1, y=="NO", y=="YES"). However, the number of NAs remains the > same after the attempt. I would like to turn all Y (NAs included) into a > "YES" or a "NO". >Hi Ana, What you probably want is: y<-ifelse(x==1,"NO","YES") Jim
On Feb 9, 2014, at 8:03 PM, Ana Genkova wrote:> Hello, > > I am trying the ifelse command for imputing a variable based on a > conditional statement, but the NAs do not transform. The code I am trying > is: > > ifelse (x==1, y=="NO", y=="YES"). However, the number of NAs remains the > same after the attempt. I would like to turn all Y (NAs included) into a > "YES" or a "NO".The "==" has no place in the second and third arguments to ifelse . It should probably be: y <- ifelse (x==1, "NO", "YES") (And do note that "==" is not an assignment operator in any context.) -- David Winsemius Alameda, CA, USA
The idea behind the treatment of NAs is that if you don't know what the value is, how do you know if it's 1 or not (in your case)? Therefore, it's normal that the result to your query is NA. First, I guess your command was supposed to be: y <- ifelse(x==1, 'NO', 'YES') Secondly, if you want to ignore NAs in your condition, do: y <- ifelse(x %in% 1, 'NO', 'YES') On 10 February 2014 15:03, Ana Genkova <ani.genkova@gmail.com> wrote:> Hello, > > I am trying the ifelse command for imputing a variable based on a > conditional statement, but the NAs do not transform. The code I am trying > is: > > ifelse (x==1, y=="NO", y=="YES"). However, the number of NAs remains the > same after the attempt. I would like to turn all Y (NAs included) into a > "YES" or a "NO". > > Thanks in advance, > > Ana > -- > Ana Genkova > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Yvan Richard * DRAGONFLY Science * *Physical address*: Level 5, 158 Victoria St, Te Aro, Wellington *Postal address*: PO Box 27535, Wellington 6141 New Zealand Ph: 04.385.9285 web page <http://www.dragonfly.co.nz> [[alternative HTML version deleted]]
Hello Ana, The syntax is: y <- ifelse(x==1, 'YES', 'NO') Hope this helps, Pascal On 10 February 2014 13:03, Ana Genkova <ani.genkova at gmail.com> wrote:> Hello, > > I am trying the ifelse command for imputing a variable based on a > conditional statement, but the NAs do not transform. The code I am trying > is: > > ifelse (x==1, y=="NO", y=="YES"). However, the number of NAs remains the > same after the attempt. I would like to turn all Y (NAs included) into a > "YES" or a "NO". > > Thanks in advance, > > Ana > -- > Ana Genkova > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Pascal Oettli Project Scientist JAMSTEC Yokohama, Japan