如何檢查R數據幀是否爲空?看看這個代碼。我想修改它,所以它不會產生錯誤或警告。檢查數據幀本身是否NA
x = sample(1:2,1)
d = NA
if(x == 1) {
d = data.frame("h"=c(1,2),"q"=c(2,3))
}
#check if data frame is NA
if(is.na(d)) {
print("d is NA")
}
如果x == 1
,然後正常工作沒有任何警告,否則,如果x == 2
下面給出警告
Warning message:
In if (is.na(d)) { :
the condition has length > 1 and only the first element will be used
是否你的意思是,如果'X == 2'它工作正常?如果你只是想禁止警告,你可以使用'suppressWarnings'。如果'd =矩陣(rep(NA,4),nrow = 2)',你希望函數返回什麼? – Hugh