Use Previous Row of data.table in R (Example) | Extract & Get Values | shift Function & type = lag

preview_player
Показать описание
R code of this video:

x2 = 7:3,
x3 = "x")

data[ , lag1 := x1 * shift(x2)] # Use previous row
data # Print updated data

data[ , lag3 := x1 * shift(x2, 3, type = "lag")] # Use three rows before
data # Print updated data

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

Thank you, you are extremely helpful, a few months ago, I need this function and was eager to use some custom variants.

OlehMezhenskyi
Автор

great video!!!
i have a question:
I´m working in an xts object with the following columns:
"date" (no real col), D_return and the new created one D_norm
The idea is that the value in D_norm should be : last value of D_norm * (1+ value from D_return)
i have the follwing code, which doesnt work:

spread_jahr_1$D_norm[1, 1] <- 1
spread_jahr_1$D_norm <- (shift(spread_jahr_1$D_norm) * (1+ spread_jahr_1$D_return))

Can you help me if possible?

franz
Автор

Hi, I have a question. In power BI I can use earlier function for Row Context operations. Is the the shift function given by the Data.Table package in r similar?

sylvestermulanshokachanda