as.function Function in R (Example)
In this tutorial, I’ll show how to create a user-defined function using the as.function function in R.
The page consists of one example for the application of the as.function function. To be more specific, the article is structured as follows:
Let’s dive right in!
Example: Create User-Defined Function Using as.function() & alist()
This example explains how to convert a object returned by the alist function to a user-defined function.
Have a look at the following R code:
my_fun <- as.function(alist(x1 = , # Using as.function & alist x2 = , x1 * x2))
The previous R syntax has constructed a user-defined function that takes two arguments x1 and x2.
Let’s apply this function to some values:
my_fun(x1 = 3, x2 = 5) # Apply user-defined function # [1] 15
The previous output shows the result when setting the function arguments to x1 = 3 and x2 = 5, i.e. the value 15.
Video, Further Resources & Summary
I have recently released a video on my YouTube channel, which explains the R programming codes of this tutorial. You can find the video below:
The YouTube video will be added soon.
Additionally, you may read the related articles on this website. I have released several tutorials already.
- is.function & is.primitive Functions in R
- Make Object Created within Function Usable Outside
- List of R Commands
- All R Programming Tutorials
At this point you should have learned how to apply the as.function function in the R programming language. Tell me about it in the comments section, if you have further questions.
Statistics Globe Newsletter