2
2
# '
3
3
# ' This is a lower-level function. As such it performs no error checking.
4
4
# '
5
- # ' @param x Data frame. Variables to lag
6
- # ' @param lags List. Each list element is a vector of lags .
5
+ # ' @param x Data frame. Variables to shift
6
+ # ' @param shifts List. Each list element is a vector of shifts .
7
7
# ' Negative values produce leads. The list should have the same
8
8
# ' length as the number of columns in `x`.
9
9
# ' @param time_value Vector. Same length as `x` giving time stamps.
10
10
# ' @param keys Data frame, vector, or `NULL`. Additional grouping vars.
11
11
# ' @param out_name Chr. The output list will use this as a prefix.
12
12
# '
13
13
# ' @return a list of tibbles
14
- epi_shift <- function (x , lags , time_value , keys = NULL , out_name = " x" ) {
14
+ epi_shift <- function (x , shifts , time_value , keys = NULL , out_name = " x" ) {
15
15
if (! is.data.frame(x )) x <- data.frame (x )
16
16
if (is.null(keys )) keys <- rep(" empty" , nrow(x ))
17
17
p_in = ncol(x )
18
- out_list <- tibble :: tibble(i = 1 : p_in , lag = lags ) %> %
19
- tidyr :: unchop(lag ) %> % # what is chop
18
+ out_list <- tibble :: tibble(i = 1 : p_in , shift = shifts ) %> %
19
+ tidyr :: unchop(shift ) %> % # what is chop
20
20
dplyr :: mutate(name = paste0(out_name , 1 : nrow(. ))) %> %
21
- # One list element for each lagged feature
22
- purrr :: pmap(function (i , lag , name ) {
21
+ # One list element for each shifted feature
22
+ purrr :: pmap(function (i , shift , name ) {
23
23
tibble(keys ,
24
- time_value = time_value + lag , # Shift back
24
+ time_value = time_value + shift , # Shift back
25
25
!! name : = x [[i ]])
26
26
})
27
27
if (is.data.frame(keys )) common_names <- c(names(keys ), " time_value" )
@@ -30,9 +30,9 @@ epi_shift <- function(x, lags, time_value, keys = NULL, out_name = "x") {
30
30
purrr :: reduce(out_list , dplyr :: full_join , by = common_names )
31
31
}
32
32
33
- epi_shift_single <- function (x , col , lag_val , newname , key_cols ) {
33
+ epi_shift_single <- function (x , col , shift_val , newname , key_cols ) {
34
34
x %> %
35
35
dplyr :: select(tidyselect :: all_of(c(key_cols , col ))) %> %
36
- dplyr :: mutate(time_value = time_value + lag_val ) %> %
36
+ dplyr :: mutate(time_value = time_value + shift_val ) %> %
37
37
dplyr :: rename(!! newname : = col )
38
38
}
0 commit comments