R Language Writing functions in R Passing column names as argument of a function


Sometimes one would like to pass names of columns from a data frame to a function. They may be provided as strings and used in a function using [[. Let's take a look at the following example, which prints to R console basic stats of selected variables:

basic.stats <- function(dset, vars){
    for(i in 1:length(vars)){

basic.stats(iris, c("Sepal.Length", "Petal.Width"))

As a result of running above given code, names of selected variables and their basic summary statistics (minima, first quantiles, medians, means, third quantiles and maxima) are printed in R console. The code dset[[vars[i]]] selects i-th element from the argument vars and selects a corresponding column in declared input data set dset. For example, declaring iris[["Sepal.Length"]] alone would print the Sepal.Length column from the iris data set as a vector.