I have a dataframe in which I want to create a new column with 0/1 (which would represent absence/presence of a species) based on the records in previous columns. I've been trying this:
update_cat$bobpresent <- NA #creating the new column
x <- c("update_cat$bob1999", "update_cat$bob2000", "update_cat$bob2001","update_cat$bob2002", "update_cat$bob2003", "update_cat$bob2004", "update_cat$bob2005", "update_cat$bob2006","update_cat$bob2007", "update_cat$bob2008", "update_cat$bob2009") #these are the names of the columns I want the new column to base its results in
bobpresent <- function(x){
if(x==NA)
return(0)
else
return(1)
} # if all the previous columns are NA then the new column should be 0, otherwise it should be 1
update_cat$bobpresence <- sapply(update_cat$bobpresent, bobpresent) #apply the function to the new column
Everything is going fina until the last string where I'm getting this error:
Error in if (x == NA) return(0) else return(1) :
missing value where TRUE/FALSE needed
Can somebody please advise me? Your help will be much appreciated.
By definition all operations on
NA
will yieldNA
, thereforex == NA
always evaluates toNA
. If you want to check if a value isNA
, you must use theis.na
function, for example:The function you pass to
sapply
expects TRUE or FALSE as return values but it gets NA instead, hence the error message. You can fix that by rewriting your function like this:In any case, based on your original post I don't understand what you're trying to do. This change only fixes the error you get with
sapply
, but fixing the logic of your program is a different matter, and there is not enough information in your post.