Use apply Function Only for Specific Data Frame Columns in R (Example)

 

In this tutorial you’ll learn how to apply a function only to certain variables of a data frame in R programming.

Table of contents:

Let’s dig in:

 

Creation of Example Data

As a first step, let’s define some example data:

data <- data.frame(var1 = 1:6,                                           # Example data
                   var2 = 1:6,
                   var3 = 1:6)
data                                                                     # Print data
#   var1 var2 var3
# 1    1    1    1
# 2    2    2    2
# 3    3    3    3
# 4    4    4    4
# 5    5    5    5
# 6    6    6    6

The previous output of the RStudio console visualizes the structure of our example data – It consists of six rows and three numeric columns. Each of the columns contains the same values ranging from 1 to 6.

 

Example: Applying Function to Specific Columns of Data Frame

The syntax below shows how to use the apply command only for a selected set of variables. First, we are creating a user-defined function that we can use within the apply() function.

my_fun <- function(x) {                                                  # Create user-defined function
  x * 2
}

Let’s assume that we want to apply our manually created function only to the first and the third column of our data frame. Then, we can use the following R syntax:

data_apply <- apply(data[ , c(1, 3)], 2, my_fun)                         # Apply function to specific columns
data_apply                                                               # Print matrix containing updated values
#      var1 var3
# [1,]    2    2
# [2,]    4    4
# [3,]    6    6
# [4,]    8    8
# [5,]   10   10
# [6,]   12   12

Have a look at the previous output: It contains only the variables that were updated with the apply function. Now, we can replace our original data with these updated values:

data_new <- data                                                         # Replicate original data
data_new[ , colnames(data_new) %in% colnames(data_apply)] <- data_apply  # Replace specific columns
data_new                                                                 # Print final data
#   var1 var2 var3
# 1    2    1    2
# 2    4    2    4
# 3    6    3    6
# 4    8    4    8
# 5   10    5   10
# 6   12    6   12

The data object data_new is our final result. As you can see, we have applied our user-defined function only to var1 and var3. The column var2 was kept in the original format.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. I show the R programming syntax of this tutorial in the video.

 

The YouTube video will be added soon.

 

Furthermore, you may want to read the related articles on https://statisticsglobe.com/.

 

In this R tutorial you learned how to use the apply function only for preliminarily selected columns. If you have any further questions, please tell me about it in the comments section. Furthermore, please subscribe to my email newsletter to get updates on the newest posts.

 

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.

Top