R – Check if given Object is a Data Frame

In this tutorial, we will learn how to check if given object is a data frame in R programming Language.

To check if given object is a data frame in R, call is.data.frame() function and pass the given object as argument to it. If the given object x is a data frame, then is.data.frame(x) returns TRUE, else it returns FALSE.

The syntax to check if object x is a data frame is

</>
Copy
is.data.frame(x)

Examples

Positive Scenario – Given object is a Data Frame

In this example, we take a data frame in x, and programmatically check if x is data frame or not using is.data.frame() function.

Example.R

</>
Copy
x <- data.frame(x1 = c("A", "B", "C"))
output <- is.data.frame(x)
if (output) {
  print("x is a data frame.")
} else {
  print("x is not a data frame.")
}

Output

[1] "x is a data frame."

Positive Scenario – Given object is not a Data Frame

In this example, we take a vector in x, and programmatically check if x is data frame or not using is.data.frame() function.

Example.R

</>
Copy
x <- c("A", "B", "C")
output <- is.data.frame(x)
if (output) {
  print("x is a data frame.")
} else {
  print("x is not a data frame.")
}

Output

[1] "x is not a data frame."

Conclusion

In this R Tutorial, we learned how to check if given object is a data frame or not using is.data.frame(), with the help of example programs.