Applying is.data.frame & as.data.frame Functions in R (2 Examples)

In this article you’ll learn how to apply the is.data.frame and as.data.frame functions in the R programming language.

Example Data

x <- matrix(1:9, nrow = 3)        # Create matrix
x
#      [,1] [,2] [,3]
# [1,]    1    4    7
# [2,]    2    5    8
# [3,]    3    6    9

Example 1: Check Class of Data Object Using is.data.frame() Function

is.data.frame(x)                  # Test if object is a data frame
# [1] FALSE

Example 2: Change matrix to data.frame Class Using as.data.frame() Function

my_df <- as.data.frame(x)         # Convert object to data frame
my_df
#   V1 V2 V3
# 1  1  4  7
# 2  2  5  8
# 3  3  6  9

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top