[R] converting NA/non-NA's to a binary variable

Gabor Grothendieck ggrothendieck at gmail.com
Sat May 7 15:02:10 CEST 2005


See ?is.na

On 5/7/05, Gillian Rutherford <gillian.rutherford at wsl.ch> wrote:
> Dear R colleagues,
> 
> I am trying to create a new column in a data frame, which converts values
> and NA's from another column into binary format. Essentially I need the
> NA's to become 1 and the rest to be 0. The code I wrote is returning the
> following error message:
> 
> Error in if (mort[i, 4] != NA) mort[i, 8] <- 0 else if (mort[i, 4] ==  :
>         missing value where TRUE/FALSE needed
> 
> The code is as follows:
> 
> for(i in 1:length(mort[,4]))
>        {
>                if(mort[i,4] != NA) mort[i,8] <- 0
>                else if(mort[i,4] == NA) mort[i,8] <- 1
>        }
> 
> I'd appreciate any advice or recommendations as to a better way of
> achieving this.
> 
> Thanks
> Gillian
> -----------------------------------------------------------------------------------------------------
> Gillian Rutherford
> Eidg. Forschungsanstalt für Wald, Schnee und Landschaft
> Swiss Federal Research Institute WSL
> Economy Section, Forest Division
> Zürcherstrasse 111
> CH - 8903 Birmensdorf
> Phone: ++ 41 1 739 26 65
> E-mail: gillian.rutherford at wsl.ch
> http://www.wsl.ch/staff/gillian.rutherford/
> 
> ______________________________________________
> 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
>




More information about the R-help mailing list