https://dplyr.tidyverse.org/reference/lead-lag.html
I want lag columns. Like above URL. But I have many features.
data <- data.frame(day=c("2010-01-01","2010-01-02","2010-01-03","2010-01-04","2010-01-05"),
dummy_1=rbinom(5,1,0.5),
dummy_2=rbinom(5,1,0.5),
dummy_3=rbinom(5,1,0.5),
#and so on ...... many dummy_X colmuns...
one_hot_1=rbinom(5,1,0.5),
one_hot_2=rbinom(5,1,0.5),
one_hot_3=rbinom(5,1,0.5)
#and so on ...... many one_hot_X colmuns...
)
day dummy_1 dummy_2 dummy_3 one_hot_1 one_hot_2 one_hot_3
1 2010-01-01 1 1 1 0 1 1
2 2010-01-02 0 1 1 0 0 0
3 2010-01-03 1 0 1 0 0 0
4 2010-01-04 0 0 1 1 1 1
5 2010-01-05 0 1 0 0 0 1
and I want to get more easily(tidy) and steady colnames.
data_2 <- mutate(data,
dummy_1_shift_2 = lag(dummy_1, 2),
dummy_1_shift_3 = lag(dummy_1, 3),
dummy_1_shift_4 = lag(dummy_1, 4),
dummy_1_shift_5 = lag(dummy_1, 5),
dummy_1_shift_6 = lag(dummy_1, 6),
dummy_1_shift_7 = lag(dummy_1, 7),
dummy_1_shift_8 = lag(dummy_1, 8),
#and so on ...... many dummy_X_shift_Y colmuns...
one_hot_shift_2 = lag(one_hot_1, 2),
one_hot_shift_3 = lag(one_hot_1, 3),
one_hot_shift_4 = lag(one_hot_1, 4),
one_hot_shift_5 = lag(one_hot_1, 5),
one_hot_shift_6 = lag(one_hot_1, 6),
one_hot_shift_7 = lag(one_hot_1, 7),
one_hot_shift_8 = lag(one_hot_1, 8)
)
do you have any idea? in R.
thank you.
We can use :
cbind(data, do.call(cbind, lapply(names(data)[-1], function(x)
setNames(do.call(cbind.data.frame, lapply(1:8, function(y)
dplyr::lag(data[[x]], y))), paste0(x, "_shift_", 1:8)))))