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/
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 >
mort[8] <- is.na(mort[4]) (If you really want 1/0, add '+ 0' to this expression.) Testing (in)equality with NA always gives NA. This is discussed in all good books on S/R, e.g. in MASS (see the FAQ). On Sat, 7 May 2005, Gillian Rutherford 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.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On 07-May-05 Gillian Rutherford 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 > GillianI think the following should do what you want, provided the column mort[,8] exists: mort[,8] <- 0 mort[is.na(mort[,4]),8] <- 1 Incidentally, tests like "== NA" or "!= NA" can produce unexpected results! Use is.na() instead: tmp<-NA ## [1] NA tmp==NA ## [1] NA tmp!=NA ## [1] NA if(tmp==NA) 1 else 2 ## Error in if (tmp == NA) 1 else 2 : ## missing value where TRUE/FALSE needed if(TRUE) 1 else 2 ## [1] 1 is.na(tmp) ## [1] TRUE if(is.na(tmp)) 1 else 2 ## [1] 1 if(!is.na(tmp)) 1 else 2 ## [1] 2 Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 07-May-05 Time: 14:46:19 ------------------------------ XFMail ------------------------------