25

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

smci
  • 32,567
  • 20
  • 113
  • 146
Agaz Wani
  • 5,514
  • 8
  • 42
  • 62

3 Answers3

44

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.

Stibu
  • 15,166
  • 6
  • 57
  • 71
  • You can check that `names(df)` is empty (`character(0)`), too. – cphlewis Feb 17 '15 at 07:32
  • 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
  • 3
    Depending 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
  • 1
    You 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
0

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.

ahmohamed
  • 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
-2

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.

  • 2
    I 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