R 检查数据帧本身是否为NA
如何检查R数据帧是否为空?查看此代码。我想修改它,这样它就不会产生错误或警告R 检查数据帧本身是否为NA,r,dataframe,na,R,Dataframe,Na,如何检查R数据帧是否为空?查看此代码。我想修改它,这样它就不会产生错误或警告 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
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
不管你的主题行是什么,看起来你真的想检查
d
是数据帧还是其他东西
if(is.data.frame(d)) {
# do something sensible with a data frame
}
else print("d is not a data frame!")
不管你的主题行是什么,看起来你真的想检查
d
是数据帧还是其他东西
if(is.data.frame(d)) {
# do something sensible with a data frame
}
else print("d is not a data frame!")
您可以将
d
设置为NULL
:
d <- NULL
d您可以将d
设置为NULL
:
d <- NULL
d您的意思是如果x==2
工作正常吗?如果只想消除警告,可以使用suppressWarnings
。如果d=matrix(rep(NA,4),nrow=2)
,您希望函数返回什么?您的意思是如果x==2它工作正常吗?如果只想消除警告,可以使用suppressWarnings
。如果d=matrix(rep(NA,4),nrow=2)
,您希望函数返回什么?+1-这正是我所建议的NULL
是更合适的未定义变量。我没有否决,但我的第一次阅读建议d确定,但MWE建议脚本在d=NA
上返回TRUE
。似乎OP已经找到了他的答案:-)+1-正是我所建议的NULL
是更合适的未定义变量。我没有否决,但我的第一次阅读建议d确定,但MWE建议脚本在d=NA
上返回TRUE
。似乎OP已经找到了答案:-)