helpers {eList} | R Documentation |
These functions help to create sequences for use in vector comprehension.
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)
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 |
n |
size of window for |
axis |
which axis to perform different operations? |
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 |
k |
number of elements to shift right. Negative values of |
g |
vector of objects used to define groups |
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", [4]]]
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) => [[2], [3], [4]]
roll(x, 2) => [[2, 4] [4, 8]]
separate(x, 2) => [[2, 4], [8, NA]]
slice(x,1,,2) => [2, 8]
splitn(y) => [[[1], [2]], [[2], [3]], [[3], [4]]]
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]]
list or other vector
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.
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))