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

Gillian Rutherford gillian.rutherford at wsl.ch
Sat May 7 14:43:26 CEST 2005


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/




More information about the R-help mailing list