-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsample-frac-n-keys.R
More file actions
60 lines (56 loc) · 1.56 KB
/
sample-frac-n-keys.R
File metadata and controls
60 lines (56 loc) · 1.56 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#' Sample a number or fraction of keys to explore
#'
#' @param .data tsibble object
#' @param size The number or fraction of observations, depending on the
#' function used. In `sample_n_keys`, it is a number > 0, and in
#' `sample_frac_keys` it is a fraction, between 0 and 1.
#'
#' @return tsibble with fewer observations of key
#' @name sample-n-frac-keys
#' @export
#' @examples
#' library(ggplot2)
#' sample_n_keys(heights,
#' size = 10) %>%
#' ggplot(aes(x = year,
#' y = height_cm,
#' group = country)) +
#' geom_line()
sample_n_keys <- function(.data, size) {
test_if_tsibble(.data)
test_if_null(.data)
UseMethod("sample_n_keys")
}
#' @export
sample_n_keys.tbl_ts <- function(.data, size) {
key_indices <- tsibble::key_rows(.data)
sample_unique_keys <- sample(key_indices, size)
dplyr::slice(.data, vctrs::vec_c(!!!sample_unique_keys))
}
#' @rdname sample-n-frac-keys
#' @examples
#' library(ggplot2)
#' sample_frac_keys(wages,
#' 0.1) %>%
#' ggplot(aes(x = xp,
#' y = unemploy_rate,
#' group = id)) +
#' geom_line()
#' @export
sample_frac_keys <- function(.data, size) {
test_if_tsibble(.data)
test_if_null(.data)
UseMethod("sample_frac_keys")
}
#' @inheritParams sample-n-frac-keys
#' @export
sample_frac_keys.tbl_ts <- function(.data, size) {
if (size > 1 & size > 0) {
stop(
"sample size for `sample_frac_keys` must between 0 and 1, the size given was `",
size,
"`"
)
}
sample_n_keys(.data, size = round(size * tsibble::n_keys(.data)))
}