Incomplete sheet
This sheet is incomplete and could use some attention. Please submit code snippet suggestions as an issue or PR here.
list
For representing a list of arbitrary objects
Create
| Action |
Code |
Details |
|
Empty list
|
|
|
|
List with single element
|
|
|
|
Define list
|
list(1, 'a', third = c(1, 2))
|
|
List of NULL values, length n
|
vector('list', length = n)
|
|
|
List of repeated scalar value v, repeated n times
|
|
Only works for scalar (length 1) input |
|
List of repeated object obj, repeated n times
|
replicate(n, obj, simplify = FALSE)
|
Also works for vector objects |
|
From vector
|
|
|
|
From string, character per element
|
strsplit('hello', '')[[1]] |> as.list()
|
|
From data.frame, as a named list of column vectors
|
|
|
From data.frame, as a list of row vectors
|
|
|
|
From environment, as name-value pairs
|
|
|
Test
| Action |
Code |
Details |
|
Is list or subclass
|
|
Also TRUE for data.frame and other list-based data structures |
|
Is list and not subclass
|
|
|
|
Empty
|
|
|
|
Not empty
|
|
|
|
Not empty
|
|
|
|
Pairwise equal to another list
|
|
|
|
Pairwise equal to another list
|
all.equal(x, y) |> isTRUE()
|
|
Contains NULL
|
sapply(x, is.null) |> any()
|
|
Contains NULL
|
vapply(x, is.null, FUN.VALUE = TRUE) |> any()
|
Fastest |
Contains NULL
|
|
Slowest |
|
Contains element e
|
|
|
|
Contains specific scalar elements
|
|
|
|
Contains specific objects
|
|
|
|
Does not contain element e
|
|
|
|
Elements are all of type t
|
sapply(x, is.numeric) |> all()
|
|
|
No duplicate elements
|
|
|
|
Has duplicate elements
|
|
|
| Action |
Code |
Details |
|
Number of elements (length)
|
|
|
|
First element
|
|
|
|
_i_th element
|
|
|
|
Last element
|
|
|
|
First element with name g, as literal
|
|
|
|
First element with name g in variable
|
|
|
|
Index of element e
|
|
Use list(e) for non-scalar elements. Returns empty vector if missing. |
Derive
Grow
| Action |
Code |
Details |
Append NULL
|
|
|
|
Append scalar element e
|
|
Not applicable for non-scalar vectors (length 0 or > 1) |
|
Insert scalar element e at the _i_th position
|
|
|
|
Replicate list n times
|
|
|
|
Replicate list to ensure length n
|
|
Cycles through elements until the length is reached |
Shrink
| Action |
Code |
Details |
|
Slice from _a_th to _b_th element
|
|
|
|
First n elements (head)
|
|
|
|
First n elements (head)
|
|
|
|
Last n elements (tail)
|
|
|
|
Drop first n elements
|
|
|
|
Drop first n elements
|
|
|
|
Drop last n elements
|
|
|
|
List of elements from a vector of indices vi
|
|
x[c(2, 3)] |
|
List of elements from a vector of names vn
|
|
`x[c('a', 'b')] |
|
Filter on condition or predicate function
|
Filter(function(e) e > 2, x)
|
|
Combine lists
| Action |
Code |
Details |
|
Concatenate lists
|
|
|
Convert
| Action |
Code |
Details |
|
To vector (flatten)
|
|
Warning: results strongly depend on element types: Elements are converted to common type. Vector elements are concatenated. Zero-length elements (e.g., NULL or integer()) are excluded. |
|
To vector (flatten), drop names
|
unlist(x, use.names = FALSE)
|
|