Reputation: 28905
How can I check if an R data frame is empty? Check out this code. I want to modify it so it never produces an error or warning.
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")
}
If x == 1
, then it works fine without any warning, otherwise, if x == 2
the following warning is given
Warning message:
In if (is.na(d)) { :
the condition has length > 1 and only the first element will be used
Upvotes: 2
Views: 6247
Reputation: 57696
Irrespective of your subject line, it looks like you really want to check if d
is a data frame or something else.
if(is.data.frame(d)) {
# do something sensible with a data frame
}
else print("d is not a data frame!")
Upvotes: 6