How to Apply the do.call() & call() Functons in R (2 Examples)
This tutorial shows how to apply the do.call and call functions in the R programming language.
Creation of Example Data
data(iris) # Load iris head(iris) # Head of iris # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Load iris head(iris) # Head of iris # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
Example 1: Using do.call Function in R
do.call("colMeans", list(iris[ , 1:4])) # Compute colMeans using do.call # Sepal.Length Sepal.Width Petal.Length Petal.Width # 5.843333 3.057333 3.758000 1.199333 |
do.call("colMeans", list(iris[ , 1:4])) # Compute colMeans using do.call # Sepal.Length Sepal.Width Petal.Length Petal.Width # 5.843333 3.057333 3.758000 1.199333
Example 2: Using call Function in R
my_call <- call("colMeans", iris[ , 1:4]) # Applying call() function |
my_call <- call("colMeans", iris[ , 1:4]) # Applying call() function
eval(my_call) # Evaluating call # Sepal.Length Sepal.Width Petal.Length Petal.Width # 5.843333 3.057333 3.758000 1.199333 |
eval(my_call) # Evaluating call # Sepal.Length Sepal.Width Petal.Length Petal.Width # 5.843333 3.057333 3.758000 1.199333