list_lengths | R Documentation |
Functions to help work with lists.
list_lengths(x, names = FALSE)
lengths_(x, names = FALSE)
unlisted_length(x)
new_list(length = 0L, default = NULL)
list_assign(x, values)
list_modify(x, values)
list_combine(..., .args = NULL)
list_drop_null(x)
x |
A list. |
names |
Should names of list elements be added? Default is |
length |
Length of list. |
default |
Default value for each list element. |
values |
A named list |
... |
Objects to combine into a list. |
.args |
An alternative to |
list_lengths()
returns the list lengths.
unlisted_length()
is a fast alternative to length(unlist(x))
.
new_list()
is like vector("list", length)
but also allows you to specify
a default value for each list element. This can be useful for
initialising with a catch-all value so that when you unlist you're guaranteed
a list of length >= to the specified length.
list_assign()
is vectorised version of [[<-
that
concatenates values
to x
or modifies x
where the
names match. Can be useful for modifying data frame variables.
list_combine()
combines each element of a set of lists into a single list.
If an element is not a list, it is treated as a length-one list.
This happens to be very useful for combining data frame cols.
list_drop_null()
removes NULL
list elements very quickly.
library(cheapr)
l <- list(1:10,
NULL,
list(integer(), NA_integer_, 2:10))
lengths_(l) # Faster lengths()
unlisted_length(l) # length of vector if we unlist
paste0("length: ", length(print(unlist(l))))
unlisted_length(l) - na_count(l) # Number of non-NA elements
# We can create and initialise a new list with a default value
l <- new_list(20, 0L)
l[1:5]
# This works well with vctrs_list_of objects
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.