# Apply Function to Each Cell of Data Frame in R (2 Examples)

In this article you’ll learn how to apply a user-defined function to each data frame element in the R programming language.

The tutorial consists of the following information:

Let’s dive into it:

## Construction of Exemplifying Data

We’ll use the following data as a basement for this R tutorial:

```data <- data.frame(x1 = 1:5,                # Create example data
x2 = letters[5:1],
x3 = 7)
data                                        # Print example data``` Table 1 shows that the example data consists of five rows and three columns.

Next, let’s create a user-defined function:

```my_fun <- function(x) {                     # Create user-defined function
paste0("out_", x)
}```

The previous R code has created a new manually defined function called my_fun. We can apply this function to a single data cell as shown below:

```my_fun(data\$x1)                          # Apply function to one value
#  "out_3"```

As you can see, our function adds the prefix “out_” to it’s input.

Let’s apply this function to all elements in our example data frame!

## Example 1: Apply User-Defined Function to Each Element of Data Frame Using lapply() Function

Example 1 shows how to apply a user-defined function to every cell of a data set using Base R.

For this task, we can use the lapply function as shown below.

Note that we are specifying [] after the name of the data frame. This keeps the structure of our data. If we wouldn’t use this operator, the lapply function would return a list object.

```data_new1 <- data                           # Duplicate data frame
data_new1[] <- lapply(data_new1, my_fun)    # Apply function to each element
data_new1                                   # Print updated data frame``` As shown in Table 2, the previous R syntax has created a new data frame called data_new1 where each cell contains the prefix “out_”.

## Example 2: Apply User-Defined Function to Each Element of Data Frame Using mutate_all() Function of dplyr Package

This example illustrates how to use the dplyr package instead of Base R to apply a function to each data cell.

We first need to install and load the dplyr package, in order to use the corresponding functions:

```install.packages("dplyr")                   # Install & load dplyr
library("dplyr")```

Next, we can apply the mutate_all function to use our function for each data cell:

```data_new2 <- data %>%                       # Apply function to each element
mutate_all(my_fun)
data_new2                                   # Print updated data frame``` Table 3 shows the output of the previous syntax: The same data frame that we have already created in Example 2.

## Video, Further Resources & Summary

Do you want to know more about the application of a user-defined function to each data frame element? Then you might want to have a look at the following video on my YouTube channel. In the video, I explain the R programming syntax of this page.

Besides the video, you might read the other articles on this website:

Summary: In this R tutorial you have learned how to apply a user-defined function to each data frame cell. Please tell me about it in the comments section below, in case you have any further comments and/or questions.

Subscribe to the Statistics Globe Newsletter
{"ft_lang_label":"__label__en","ft_lang_prob":0.7220218,"math_prob":0.6877337,"size":3269,"snap":"2023-40-2023-50","text_gpt3_token_len":755,"char_repetition_ratio":0.17366003,"word_repetition_ratio":0.06842105,"special_character_ratio":0.23524013,"punctuation_ratio":0.078904994,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9857338,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-09T05:08:33Z\",\"WARC-Record-ID\":\"<urn:uuid:3e449369-5778-42c8-8529-7616652353be>\",\"Content-Length\":\"214249\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:623c6225-58a4-4245-a1dd-a96e8513ee4f>\",\"WARC-Concurrent-To\":\"<urn:uuid:41fbe678-bcb7-4425-8e13-0574065259cb>\",\"WARC-IP-Address\":\"217.160.0.159\",\"WARC-Target-URI\":\"https://statisticsglobe.com/apply-function-each-cell-data-frame-r\",\"WARC-Payload-Digest\":\"sha1:7HGH73K2QQRPVU7B3CVUYN7ZSC7IBSDL\",\"WARC-Block-Digest\":\"sha1:RYRQDTNTJCQ2VBFM3SU55AE2GQB4YVT6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100800.25_warc_CC-MAIN-20231209040008-20231209070008-00330.warc.gz\"}"}