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

From: Gillian Rutherford <gillian.rutherford_at_wsl.ch>
Date: Sat 07 May 2005 - 22:43:26 EST


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 Received on Sat May 07 22:49:20 2005

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