Skip to content
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()
List with single element
list('hello')
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
rep(v, n) |> as.list()
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
as.list(vec)
From string, character per element
strsplit('hello', '')[[1]] |> as.list()
From data.frame, as a named list of column vectors
as.list(df)
From data.frame, as a list of row vectors
split(df, seq(nrow(df)))
From environment, as name-value pairs
as.list(env)

Test

Action Code Details
Is list or subclass
is.list(x)
Also TRUE for data.frame and other list-based data structures
Is list and not subclass
is(x, 'list')
Empty
length(x) == 0
Not empty
length(x) > 0
Not empty
assertthat::not_empty(x)
Pairwise equal to another list
identical(x, y)
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
list(NULL) %in% x
Slowest
Contains element e
e %in% x
Contains specific scalar elements
all(c(1, 2) %in% x)
Contains specific objects
all(list(1, 2) %in% x)
Does not contain element e
not(e %in% x)
Elements are all of type t
sapply(x, is.numeric) |> all()
No duplicate elements
len(x) == len(unique(x))
Has duplicate elements
len(x) != len(unique(x))

extract

Action Code Details
Number of elements (length)
length(x)
First element
x[[1]]
_i_th element
x[[i]]
Last element
tail(x, 1)
First element with name g, as literal
x$g
First element with name g in variable
x[g]
Index of element e
which(x %in% e)
Use list(e) for non-scalar elements. Returns empty vector if missing.

Derive

Grow

Action Code Details
Append NULL
append(x, list(NULL))
Append scalar element e
append(x, e)
Not applicable for non-scalar vectors (length 0 or > 1)
Insert scalar element e at the _i_th position
append(x, e, after = i)
Replicate list n times
rep(x, n)
Replicate list to ensure length n
rep_len(x, n)
Cycles through elements until the length is reached

Shrink

Action Code Details
Slice from _a_th to _b_th element
x[a:b]
First n elements (head)
head(x, n)
First n elements (head)
x[1:n]
Last n elements (tail)
tail(x, n)
Drop first n elements
tail(x, -n)
Drop first n elements
x[-1:-n]
Drop last n elements
head(x, -n)
List of elements from a vector of indices vi
x[vi]
x[c(2, 3)]
List of elements from a vector of names vn
x[vn]
`x[c('a', 'b')]
Filter on condition or predicate function
Filter(function(e) e > 2, x)

Combine lists

Action Code Details
Concatenate lists
c(x, y, z)

Convert

Action Code Details
To vector (flatten)
unlist(x)
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)