gen.named.list: Generate Named Lists, Vectors, Data Frames, and Matrices with...

Description Usage Arguments Details Value See Also Examples

View source: R/gen-list.r

Description

Functions to transform patterns with placeholders into characters or into names of lists, vectors, data frames or matrices, based on variable ranges and additional conditions.

Usage

1
2
3
4
5
6
7
gen.named.list(.str, .expr, ...)

gen.named.vector(.str, .expr, ...)

gen.named.data.frame(.str, .expr, ..., byrow = FALSE)

gen.named.matrix(.str, .expr, ..., byrow = FALSE)

Arguments

.str

A character, containing expressions to be evaluated in {}-brackets, e.g., "a{x}" is transformed into "a1" for x = 1. Double brackets are transformed into a single bracket without evaluating the inner expression. For instance, "var{x + 1}_{{a}}" is transformed into "var2_{a}" for x = 1.

.expr

A base expression containing free variables which is evaluated for all combinations of variables.

...

Arbitrary many variable ranges and conditions.

byrow

Logical. If FALSE (the default), the elements of an .expr vector are taken as columns. Otherwise, they are taken as rows.

Details

The free variables in the inner expressions (i.e., the content of the {}-brackets) of .expr are evaluated in the same way as expressions in gen.list.

See gen.list for more details on the .expr and ... parameters.

Value

These functions return lists, vectors, data frames, and matrices. They work very similar to their counterparts without ".named". Additionally the vector of characters, induced by .str, serves as a vector of names for the generated structures. In case of lists or vectors, the result is a named list or a named vector. For data frames and matrices, the names are taken as row names.

See Also

gen.list for explanations on list and vector comprehension, and listcompr for an overview of all list comprehension functions.

Examples

1
2
3
4
5
6
7
8
9
# sum up 1:i for i in 1:5
gen.named.list("sum_to_{x}", sum(1:x), x = 1:5)

# matrix with named columns and rows
gen.named.matrix("row{i}", gen.named.vector("col{j}", i+j, j = 1:3), i = 1:3)

# a matrix where the expression refers to the rows and not the columns
gen.named.matrix("col{i}", c(row1 = i, row2 = 10 * i, row3 = 100 * i), i = 1:10,
                 byrow = TRUE)

listcompr documentation built on Oct. 2, 2021, 5:06 p.m.