helpers {eList} R Documentation

## Helpers for Vector Comprehension

### Description

These functions help to create sequences for use in vector comprehension.

### Usage

```items(x)

vals(x)

enum(x)

rows(x, ...)

cols(x, ...)

zip(..., fill = NA, longest = TRUE)

lrep(x, n = 2, axis = 0)

transpose(x, fill = NA, longest = TRUE)

slice(x, start, end, by = 1L)

roll(x, n = 2, fill = NULL, head = TRUE, ...)

unroll(x)

lagg(x, k = 1, fill = NA, axis = 0)

groups(x, g)

chars(x)

chain(x)

separate(x, n = 2, fill = NA)

first(x)

rest(x)

splitn(x, n = 1)
```

### Arguments

 `x` list, environment, or other vector `...` vectors to combine `fill` object with which to fill the vector when operating on elements with varying lengths or shifts. `longest` logical; should the longest item be used to determine the new length or shortest? Defaults to `TRUE`. `n` size of window for `roll` and `separate`, or position of item in which to split each element in `splitn` `axis` which axis to perform different operations? `axis=0`, the default, performs operations on each element in the list (columns), while `axis=1` performs operations on each object within the elements of a list (rows). `start, end, by` integers of length 1 describing the sequence for slicing the vector. If missing, they will default to the start or end of the vector. `head` logical; should `fill` be at the head of the vector or the tail? `k` number of elements to shift right. Negative values of `k` shift to the left `g` vector of objects used to define groups

### Details

These functions transform vectors or other objects into lists, by adding elements, grouping objects, extracting certain elements, and so forth. These can be used in conjunction with vector `comprehension` to develop quick and readable code.

An example of how each of these can be used is seen here. Let `x` and `y` be given as follows.

`x = list(a = 2, b = 4, c = 8)` `y = list(1:2, 2:3, 3:4)`

Then the various helper functions will have the following effect.

• `chain(y) => [1, 2, 2, 3, 3, 4]`

• `chars("hello") => ['h', 'e', 'l', 'l', 'o']`

• `enum(x) => [[1, 2], [2, 4], [3, 8]]`

• `first(y) => [1, 2, 3]`

• `groups(x, c("z", "w", "z")) => [["z", [2, 8]], ["w", ]]`

• `items(x) => [["a", 2], ["b", 4], ["c", 8]]`

• `lagg(x, 2) => [[2, 4, 8], [NA, 2, 4], [NA, NA, 2]]`

• `lrep(x, 3) => [[2, 4, 8], [2, 4, 8], [2, 4, 8]]`

• `rest(y) => [, , ]`

• `roll(x, 2) => [[2, 4] [4, 8]]`

• `separate(x, 2) => [[2, 4], [8, NA]]`

• `slice(x,1,,2) => [2, 8]`

• `splitn(y) => [[, ], [, ], [, ]]`

• `transpose(y) => [[1, 2, 3], [2, 3, 4]]`

• `unroll(y) => [1, 2, 3, 4]`

• `vals(x) => [2, 4, 8]`

• `zip(x, 1:3) => [[2, 1], [4, 2], [8, 3]]`

### Value

list or other vector

### Functions

• `items`: Create a list containing the name of each element of `x` and its value.

• `vals`: Extract the values of x without their names.

• `enum`: Create a list containing the index of each element of `x` and its value.

• `rows`: Create a list containing the rows of a data.frame or matrix

• `cols`: Create a list containing the columns of a data.frame or matrix

• `zip`: Merge two or more vectors into a list with each index containing values from each vector at that index.

• `lrep`: Repeat `x`, `n` times, with each repetition being an item in a list.

• `transpose`: Transpose a list or other object into a list. Opposite of `zip`.

• `slice`: Subset an object by a sequence: `start`, `end`, `by`. If `start` is missing, it is assumed to be 1. If `end` is missing, it is assumed to be the length of the object.

• `roll`: Create a list of objects containing `n` items from `x`, with `n-1` elements overlapping in a chain. Opposite of `unroll`.

• `unroll`: Flatten a list by combining the unique elements between each group of two elements. Opposite of `roll`.

• `lagg`: Create a list containing an object and each the first `k` lags of an object.

• `groups`: Create a list where each element is a list with the first element equal to a unique value in `g` and the other element is a list containing all values of `x` at the same indices as the value of `g`.

• `chars`: Convert a character string into a vector of single character values.

• `chain`: Combine each object in a list. Opposite of `separate`.

• `separate`: Separate vector into a list of objects with length `n`. Opposite of `chain`.

• `first`: Take the first element of each item in a list.

• `rest`: Remove the first element of each item in a list.

• `splitn`: Split each element in a list into two parts: one with the first `n` elements and the second with the rest.

### Examples

```x <- 1:10
y <- 32:35

n <- Num(for (i.j in zip(x,y)) i+j)
# Note that the result is different from x+y since the shortest does not repeat
mean(n[1:4])

e <- new.env()
e\$a <- 1:5
e\$b <- 6:10

e2 <- Env(for (key.val in items(e)) key = sqrt(val))
e2\$a

# row product
mat <- matrix(1:9, nrow=3)
Num(for (i in rows(mat)) prod(i))
```

[Package eList version 0.2.0 Index]