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

 

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

Table of contents:

With that, let’s start right away…

 

Example 1: Test if Data Object has the data.frame Class Using is.data.frame() Function

The following R syntax explains how to check whether a data object has the data.frame class using the is.data.frame function.

For this example, we first have to create a data object:

mat <- matrix(10:25, ncol = 4)    # Create matrix
mat                               # Print matrix

 

table 1 matrix is data frame as data frame functions r

 

In Table 1 it is shown that we have created a new data object containing four rows and four columns by running the previous R code.

Next, we can apply the is.data.frame function to return a logical indicator that shows if our data object has the data.frame class:

is.data.frame(mat)                # Apply is.data.frame function
# [1] FALSE

As you can see, the RStudio console has returned the logical value FALSE, i.e. our data object is not a data frame.

 

Example 2: Convert Matrix to Data Frame Using as.data.frame() Function

This example explains how to convert a matrix object to a data frame using the as.data.frame function.

For this, we have to note that the data object that we have created in Example 1 actually is a matrix.

We can transform this matrix to a data frame as shown in the following syntax:

data1 <- as.data.frame(mat)       # Apply as.data.frame function
data1                             # Print data frame

 

table 2 data frame is data frame as data frame functions r

 

As shown in Table 2, the previous code has created a data object containing the same values as our input matrix.

Let’s apply the is.data.frame function once again to test the class of our new data object:

is.data.frame(data1)              # Apply is.data.frame function
# [1] TRUE

This time, the is.data.frame has returned the logical indicator TRUE, i.e. our updated data object has the data.frame class.

 

Example 3: Convert dplyr Tibble to Data Frame Using as.data.frame() Function

Alternatively to matrices, it’s also possible to use the as.data.frame function to convert other data types to the data.frame class

In Example 3, I’ll illustrate how to switch from the tibble class of the tidyverse to the data.frame class.

For this example, we need the functions provided by the dplyr package.

To be able to apply the functions of the dplyr package, we first have to install and load dplyr:

install.packages("dplyr")         # Install dplyr package
library("dplyr")                  # Load dplyr package

In the next step, we can use the as_tibble function ton convert our previously created data frame to a tibble:

tibb <- as_tibble(data1)          # Create tibble
tibb                              # Print tibble

 

table 3 tbl_df is data frame as data frame functions r

 

The output of the previous R code is shown in Table 3 – A tibble with the same values as our input data.

We can confirm that by applying the class function to our new data object:

class(tibb)                       # Print class of tibble
# [1] "tbl_df"     "tbl"        "data.frame"

Now, we can apply the as.data.frame function to this tibble to convert it back to the data.frame class:

data2 <- as.data.frame(tibb)      # Apply as.data.frame function
data2                             # Print data frame

 

table 4 data frame is data frame as data frame functions r

 

The output of the previous R code is shown in Table 4. Once again our data has the same values. However, this time we are dealing with a data frame, as we can see by running the class function once again:

class(data2)                      # Print class of tibble
# [1] "data.frame"

Note that we could apply the as.data.frame function to other classes such as the data.table class as well.

 

Video & Further Resources

I have recently released a video on the Statistics Globe YouTube channel, which illustrates the R programming codes of this page. You can find the video instruction below.

 

The YouTube video will be added soon.

 

Besides the video, you might want to have a look at some of the related tutorials on this website.

 

At this point you should have learned how to use the is.data.frame and as.data.frame functions in R programming. Tell me about it in the comments section, if you have any additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Menu
Top