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.

Value

list or other vector

Functions

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]