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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 07 May 2005 - 23:02:10 EST

See ?is.na

On 5/7/05, Gillian Rutherford <gillian.rutherford@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@wsl.ch
> http://www.wsl.ch/staff/gillian.rutherford/
>
> ______________________________________________
> R-help@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
>



R-help@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 Received on Sat May 07 23:07:46 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:38 EST