Description Usage Arguments Value Mathematical operations Examples
Compute the lengths and indices of runs of NA
in a vector – or the reverse
operation.
1 2 3 4 5 6 7 8 9 10 11 12  na_rle(x = double(), index_by = seq_along(x), interval = NULL)
list_of_na_rle(x = double(), index_by = seq_along(x),
interval = NULL)
na_rle_inverse(x)
na_rle_lengths(x)
na_rle_starts(x)
na_rle_ends(x)

x 
A vector. 
index_by 
A vector of the same length as 
interval 
if 
An object of class rle_na
or list_of_rle_na
. A named list of:
lengths
: the lengths of NA
runs
indices
: the starting indices of runs
Many math operations can be applied to objects returned from na_rle()
and
list_of_na_rle()
, regarding the lengths of runs.
sum()
: the total number of NA
over all runs.
mean()
: the average NA
s per run.
min()
& max()
: the minimum and maximum of runs.
median()
& quantile()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  library(dplyr, warn.conflicts = FALSE)
df < tibble(year = 2000:2019, temp = sample(0:30, size = 20))
df[c(1, 6, 13:16, 19), "temp"] < NA
df
na_rle(df$temp) # indexed by the default positions
(x < na_rle(df$temp, index_by = df$year)) # indexed by a variable
# getters
na_rle_inverse(x)
na_rle_lengths(x)
na_rle_starts(x)
na_rle_ends(x)
# subsetting
x[1:2]
# math operations
length(x) # the number of runs
sum(x) # the total number of `NA`
range(x) # min & max runs
# list_of_na_rle() is useful when working with tabular data
na_rle_df < df %>%
mutate(group = rep(letters[1:2], each = 10)) %>%
group_by(group) %>%
summarise(na_runs = list_of_na_rle(temp, year))
na_rle_df
na_rle_inverse(na_rle_df$na_runs)
sum(na_rle_df$na_runs)
range(na_rle_df$na_runs)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.