Pass Data Frame Column Name to Function in R (Example)
In this R tutorial you’ll learn how to use variable names in manually created functions.
Table of contents:
- Creating Example Data
- Example: Use Variable Names in User-Defined R Function
- Video, Further Resources & Summary
Let’s dive right in:
Creating Example Data
First, we need to create some example data that we can use in the tutorial later on:
my_data <- data.frame(x1 = 1:5, # Create example data x2 = LETTERS[1:5], x3 = 1) my_data # Return data to RStudio console # x1 x2 x3 # 1 1 A 1 # 2 2 B 1 # 3 3 C 1 # 4 4 D 1 # 5 5 E 1
The previous R code created a data frame with five rows and three variables x1, x2, and x3. Now, let’s create a user-defined function, which uses these columns…
Example: Use Variable Names in User-Defined R Function
The following R syntax creates a user-defined function in R. Note that we are defining data_input and columns as input arguments for the function.
my_fun <- function(data_input, columns) { # Write user-defined function data_output <- data_input[ , columns] # At this point of the function, do with the columns whatever you want... return(data_output) }
Now, we can apply the function by specifying our example data as data_input and the variable names we want to use as columns:
my_fun(data_input = my_data, columns = c("x1", "x3")) # Apply user-defined function # x1 x3 # 1 1 1 # 2 2 1 # 3 3 1 # 4 4 1 # 5 5 1
As you can see, our function just returns the two specified column names. However, you may do whatever you want within your own user-written function.
Video, Further Resources & Summary
Would you like to know more about manually created functions in R? Then you may want to have a look at the following video of my YouTube channel. In the video, I show the R code of this article in a programming session:
The YouTube video will be added soon.
In addition, you may read some of the related tutorials of https://statisticsglobe.com/. You can find a selection of tutorials here.
Summary: In this R programming tutorial you learned how to pass column names as argument to a manual function. Let me know in the comments section, in case you have further questions.
Statistics Globe Newsletter