我有一個數據幀,我想根據前面的列中的記錄創建一個新的列,其中包含0/1(表示不存在/存在物種)。我一直想這樣的:基於幾列創建具有二進制數據的新列
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功能到新列
一切國際泳聯下去,直到最後一個字符串,我」 m得到這個錯誤:
Error in if (x == NA) return(0) else return(1) :
missing value where TRUE/FALSE needed
有人可以請教我嗎? 您的幫助將不勝感激。
這是哪一種語言? R' – 2013-04-25 18:56:32
對不起,在R – Cat 2013-04-25 18:59:28
嘗試''update_cat $ bobpresent < - ifelse(all(is.na(update_cat [,x])),1,0)'其中x只是列名'c(「bob1999」, 「bob2000」,「bob2001」)..... – haki 2013-04-25 19:08:57