如何检查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