na-rle: Run lengths encoding for missing values ('NA')

Description Usage Arguments Value Mathematical operations Examples

Description

Compute the lengths and indices of runs of NA in a vector – or the reverse operation.

Usage

 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)

Arguments

x

A vector.

index_by

A vector of the same length as x.

interval

if NULL, determined by the greatest common denominator; otherwise a supplied "interval" class. See ?tsibble::tsibble for details.

Value

An object of class rle_na or list_of_rle_na. A named list of:

Mathematical operations

Many math operations can be applied to objects returned from na_rle() and list_of_na_rle(), regarding the lengths of runs.

Examples

 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)

earowang/mists documentation built on Sept. 21, 2019, 1:12 p.m.