Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a variable length `alist()`

Tags:

r

In this Answer, an alist() is proposed as an easy means of creating a list with empty elements. A use case for this would be constructing a list suitable for a call to [ arranged via do.call(). For example:

x <- matrix(1:6, ncol = 2)
do.call(`[`, alist(x, , 2)) ## extract column 2 of x

[1] 4 5 6

The particular Question prompting the alist() Answer required the setting of the empty arguments dynamically on basis of an object shortdim.

If one knew how many dimensions were present one could do

al <- alist( , , ) ## 3 arguments for a 2-d object
al[[1]] <- x
shortdim <- 1
al[[shortdim + 1]] <- 1:2 ## elements 1 & 2 of dim shortdim, plus all other dims
do.call(`[`, al) 

> do.call(`[`, al) 
     [,1] [,2]
[1,]    1    4
[2,]    2    5
> x[1:2, ]         ## equivalent too
     [,1] [,2]
[1,]    1    4
[2,]    2    5

A list of dynamic length can be created by vector(), eg

ll <- vector(mode = "list", length = length(dim(x)) + 1)

But an alist can't be made in that way

> vector(mode = "alist", length = length(dim(x)) + 1)
Error in vector(mode = "alist", length = length(dim(x)) + 1) : 
  vector: cannot make a vector of mode 'alist'.

Is there a way to create an alist of dynamic length that can be filled in later where required?

like image 448
Gavin Simpson Avatar asked Jul 19 '13 17:07

Gavin Simpson


People also ask

How do I create a variable-length list in Python?

To pass a variable number of arguments to a function in Python, use the special syntax *args in the function specification. It is used to pass a variable-length, keyword-free argument list. By convention, the sign * is frequently used with the word args in the syntax for taking in a variable number of arguments.

Can you use LEN () on a list?

The function len() is one of Python's built-in functions. It returns the length of an object. For example, it can return the number of items in a list. You can use the function with many different data types.

How do you create a variable list in Python?

In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.).

How do you calculate len of a list?

Technique 1: The len() method to find the length of a list in Python. Python has got in-built method – len() to find the size of the list i.e. the length of the list. The len() method accepts an iterable as an argument and it counts and returns the number of elements present in the list.


2 Answers

OK, I'll bite. I would probably use list(bquote()) to construct a one element list containing the empty symbol, and rep that out to the desired length.

n <- 2
rep(list(bquote()), n)
# [[1]]
# 
# 
# [[2]]
# 
# 

As a bonus, here is a collection of 5 ways to create/access the empty symbol that is needed as the content of each list element:

bquote()
# 
substitute()
# 
quote(expr= )
# 
formals(function(x) {})$x
# 
alist(,)[[1]]
# 
like image 110
Josh O'Brien Avatar answered Sep 30 '22 12:09

Josh O'Brien


Thanks to Ferdinand Kraft:

# no. of elements in the alist
n <- 5
a <- rep(alist(,)[1], n)
like image 43
Hong Ooi Avatar answered Sep 30 '22 10:09

Hong Ooi