How can I check if a data frame is empty in R, and can you provide an example?

How can I check if a data frame is empty in R, and can you provide an example?

To check if a data frame is empty in R, you can use the “nrow” function which returns the number of rows in a data frame. If the number of rows returned is zero, then the data frame is considered empty. An example of this would be:

# Creating an empty data frame
df

Check if Data Frame is Empty in R (With Example)


The fastest way to check if a data frame is empty in R is to use the nrow() function:

nrow(df)

This function returns the number of a rows in a data frame.

If the function returns 0, then the data frame is empty.

If you’d like to check if a data frame is empty in an if else function, you can use the following syntax to do so:

#create if else statement that checks if data frame is empty
if(nrow(df) == 0){
  print("This data frame is empty")
}else{
  print("This data frame is not empty")
}

The following example shows how to check if a data frame is empty in practice.

Related:

Example: Check if Data Frame is Empty in R

Suppose we create the following data frame in R that has three columns but is completely empty:

#create empty data frame
df <- data.frame(player = character(),
                 points = numeric(),
                 assists = numeric())

#view data frame
df

[1] player  points  assists
<0 rows> (or 0-length row.names)

We can use the nrow() function to check how many rows are in the data frame:

#display number of rows in data frame
nrow(df)

[1] 0

Since the function returns 0, this tells us that the data frame is empty.

We can also use the following if else statement to tell us whether or not the data frame is empty:

#create if else statement that checks if data frame is empty
if(nrow(df) == 0){
  print("This data frame is empty")
}else{
  print("This data frame is not empty")
}

[1] "This data frame is empty"

From the output we can see that the data frame is indeed empty.

Cite this article

stats writer (2024). How can I check if a data frame is empty in R, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-check-if-a-data-frame-is-empty-in-r-and-can-you-provide-an-example/

stats writer. "How can I check if a data frame is empty in R, and can you provide an example?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-check-if-a-data-frame-is-empty-in-r-and-can-you-provide-an-example/.

stats writer. "How can I check if a data frame is empty in R, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-check-if-a-data-frame-is-empty-in-r-and-can-you-provide-an-example/.

stats writer (2024) 'How can I check if a data frame is empty in R, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-check-if-a-data-frame-is-empty-in-r-and-can-you-provide-an-example/.

[1] stats writer, "How can I check if a data frame is empty in R, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I check if a data frame is empty in R, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top