listcompr v0.1.0


Monthly downloads



List Comprehension for R

Syntactic shortcuts for creating synthetic lists, vectors, and data frames using list comprehension.



The listcompr package is a light-weight collection of functions for list comprehension. It is intended as “syntactic sugar” for R and it is inspired by the list comprehension capabilities from 'python'. Next to lists, similar structures like vectors (of numeric or character type), data frames, or named lists can be easily composed. The package may be used for the simple generation of small data sets for “textbook examples”, for unit tests of your R code, or for tiny mathematical tasks.


To install the latest version from CRAN, simply use:


To install and load the latest development version from this repository, execute the following in R:


install_github("patrickroocks/listcompr", build_vignettes = TRUE)


Tiny examples

We show the basic functionality to compose lists, vectors and data frames. The first argument for each generator function (gen.list, gen.vector, and is a base expression, and the other arguments are variable ranges and conditions.

First, we want to get a vector of all numbers in 1:10 which can be divided by 3 or 4:

gen.vector(i, i = 1:10, i %% 3 == 0 || i %% 4 == 0)
## Returns: c(3, 4, 6, 8, 9)

Next we want to compose a list of tuples c(i, j) where i and j are from 1:3 andi <= j holds:

gen.list(c(i, j), i = 1:3, j = i:3)
## Returns: list(c(1, 1), c(1, 2), c(2, 2), c(1, 3), c(2, 3), c(3, 3))

There is also a function to compose a data frame which expects a named vector as base expression. For example we can easily sum up 1:i while iterating over i: = a, sum = sum(1:a)), a = 1:10)

The first three lines of this data frame are:

        n sum
    1   1   1
    2   2   3
    3   3   6

Nested calculations and named lists

In listcompr, the list and vector compositions can be nested. We create a vector containing all "perfect numbers" between 2 and 100, i.e., numbers where the sum of the divisors equals the number:

gen.vector(a, a = 2:100, a == sum(gen.vector(x, x = 1:(a-1), a %% x == 0)))
## Returns: c(6, 28)

There are also functions to compose characters. For instance the package offers a function gen.named.list, where the first argument is the name (expressions in {}-brackets are substituted) of each list entry. The remaining arguments are the same as for gen.list. The following statement gives us all the divisors between 5 and 10:

gen.named.list('divisors_of_{a}', gen.vector(x, x = 1:(a-1), a %% x == 0), a = 5:10)

The last list entry has the name 'divisors_of_10' and the content c(1, 2, 5).

More examples

After installation of the package, run

vignette("introduction", package = "listcompr")

to see a vignette with some more examples.


To submit bugs or suggest improvements, feel free to write a mail to me: Patrick Roocks,

Functions in listcompr

Name Description
gen.logical.and Generate Logical Conditions with List Comprehension
gen.list.expr Generate List and Vector Expressions with List Comprehension
gen.list Generate Lists, Vectors and Data Frames with List Comprehension
listcompr Summary of the listcompr Package
gen.list.char Generate Characters and Named Lists, Vectors and Data Frames with List Comprehension
No Results!

Vignettes of listcompr

No Results!

Last month downloads


Date 2021-01-10
License GPL (>= 2)
LazyData true
Collate 'listcompr.r' 'gen-list.r'
VignetteBuilder knitr
RoxygenNote 7.1.1
Encoding UTF-8
NeedsCompilation no
Packaged 2021-01-10 12:09:52 UTC; patrick
Repository CRAN
Date/Publication 2021-01-13 16:00:02 UTC

Include our badge in your README