please excuse me if my question is quite basic.
I created an empty data frame by df <- data.frame()
and obviously the data frame is NULL (empty). when I try to check if the data frame is empty by is.null(df)
, the result comes FALSE. Is there any difference between NULL
and empty
in R. In this case if the data frame is not NULL
, then what is in the empty data frame and when it will be NULL
. Thanks
-
1`df` exists, so it isn't null. `is.empty(df)` is TRUE, though. – cphlewis Feb 17 '15 at 07:22
-
@cphlewis From where comes `is.empty`? – Feb 17 '15 at 07:23
-
1Oops, sorry, I guess it's from `spatstat`! – cphlewis Feb 17 '15 at 07:26
-
@cphlewis. Error: could not find function "is.empty" – Agaz Wani Feb 17 '15 at 07:27
-
there no such function in R – Agaz Wani Feb 17 '15 at 07:29
-
It is `spatstat` package, as @cphlewis said above. – Feb 17 '15 at 07:32
-
@AaghazHussain: you have to install spatstat package. Then the function is [spatstat::is.empty()](http://www.inside-r.org/packages/cran/spatstat/docs/is.empty) It's not a builtin function. – smci Feb 17 '15 at 07:32
-
@cphlewis May I know when `is.null()` is going to be `TRUE`. – Agaz Wani Feb 17 '15 at 07:39
-
`is.null(NULL)` or `x <- NULL; is.null(x)`. As far as I know, there is no other use of `is.null()`. – Feb 17 '15 at 07:40
-
@Pascal, My argument in case of data frame. Storing `NULL` and then checking will always lead to `TRUE`. – Agaz Wani Feb 17 '15 at 07:46
-
2As soon as a variable is a data frame it can't be NULL because it has some contents: it has a class. The same is true for all other classes. For instance, in `a<-numeric()`, `a` is empty but not NULL. – Stibu Feb 17 '15 at 07:55
3 Answers
df
is not NULL because it is a data frame and thus has some defined properties. For instance, it has a class. And you can get the number of rows in the data frame using nrow(df)
, even if the result should happen to be zero. Therefore, also the number of rows is well-defined.
As fas as I know, there is no is.empty
command in base R. What you could do is, e.g., the following
is.data.frame(df) && nrow(df)==0
This will give TRUE for an empty data frame (that is, one with no rows) and false otherwise.
The reason for checking is.data.frame
first is that nrow
might cause an error, if it is applied to anything else than a data frame. Thanks to &&
, nrow(df)
will only be evaluated if df
is a data frame.

- 15,166
- 6
- 57
- 71
-
-
its checking for rows of df by `nrow(df)==0` which is obviously of `0` and no need to write ` is.data.frame(df)` – Agaz Wani Feb 17 '15 at 07:34
-
3Depending on how you define empty you cannot just check `names(df)`. The following data frame `df<-data.frame(a=numeric(),b=numeric())` has not contents, but the names are nevertheless defined. So my definition would say it's empty, yours would say it's not. All a matter of what you acutally want to check, of course. – Stibu Feb 17 '15 at 07:38
-
1You need to check `is.data.frame` if you want to avoid strange behaviour in case that `df` is not a data frame. If you are sure that it will always be a data frame, then you can omit `is.data.frame`. – Stibu Feb 17 '15 at 07:40
data.frame()
creates an object that has a data frame class. Because the object exists, is.null
will return FALSE. A NULL
variable has no class and no contents.

- 2,920
- 20
- 35
-
1`is.null` will return `FALSE` because the object isn't NULL; because the object *exists*, `is.null` won't return "Error: object not found." – cphlewis Feb 18 '15 at 01:35
Above answers are correct, is.na and is.null couldn't not detect empty value in R. This is what I would do to calculate how many empty value you have in your data frame 'df' in this case.
is.na(df[df =='']) <- TRUE # this just replace NA to the empty value in df.
sum(is.na(df)) # would give you an idea how many empty values you have in your 'df'.
Hope this is helpful.

- 19
- 2
-
2I don't think that's actually what the OP asked. I don't want to discourage you from answering questions on SO, but this answer (while it answers *someone's* question) is too far from the question to be useful ... – Ben Bolker Jun 10 '16 at 15:40