& Functions in R (3 Examples)


In this tutorial you’ll learn how to apply the and 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 Function

The following R syntax explains how to check whether a data object has the data.frame class using the 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 function to return a logical indicator that shows if our data object has the data.frame class:                # Apply 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 Function

This example explains how to convert a matrix object to a data frame using the 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 <-       # Apply 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 function once again to test the class of our new data object:              # Apply function
# [1] TRUE

This time, the 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 Function

Alternatively to matrices, it’s also possible to use the 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 function to this tibble to convert it back to the data.frame class:

data2 <-      # Apply 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 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.



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 and 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.