alist() Function in R (2 Examples)


In this article, I’ll illustrate how to create an alist object using the alist() function in the R programming language.

The content of the page looks as follows:

Let’s jump right to the examples!


Example 1: Construct List of Function Arguments Using alist()

The following R programming code explains how to create a list of function arguments using the alist function in R.

Consider the R syntax below:

my_alist <- alist(x = , y = 5, x^2 + y)    # Create alist object
my_alist                                   # Print alist object
# $x
# $y
# [1] 5
# [[3]]
# x^2 + y

As you can see based on the previous output, we have created a list containing three elements. The first list element contains the x argument, the second list element contains the y argument and its default value (i.e. 5), and the third list element contains the function body.


Example 2: Convert alist() Output to User-Defined Function

In this example, I’ll illustrate how to convert a list created by the alist command to a user-defined function.

For this task, we can apply the as.function function as shown below:

my_fun <- as.function(my_alist)            # Convert alist object to function
my_fun                                     # Print user-defined function
# function (x, y = 5) 
# x^2 + y
# <environment: 0x55832aa301c8>

The previous output shows the structure of our new function called my_fun.

Let’s apply this function to a certain x-value (i.e. 2):

my_fun(x = 2)                              # Apply user-defined function
# [1] 9

The result of our user-defined function is 9.


Video, Further Resources & Summary

Have a look at the following video on my YouTube channel. In the video, I’m explaining the content of this tutorial:


The YouTube video will be added soon.


Furthermore, you could have a look at some of the related articles on this homepage. I have released several articles already:


To summarize: At this point you should know how to use the alist() function in the R programming language. If you have further questions, let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter to get updates on the newest articles.


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.