Suppose that I have a dataframe like the following:
library(tidyverse)
df <- tibble(x = c(1,2,3), y = c(4,5,6))
# A tibble: 3 x 2
x y
<dbl> <dbl>
1 1 4
2 2 5
3 3 6
And I would like to shift a column, adding a column like:
# A tibble: 3 x 3
x y shifted_x
<dbl> <dbl> <dbl>
1 1 4 NA
2 2 5 1
3 3 6 2
Basically, I want to work with timeseries, so I would like to get previous values and use them as feature. In python
I know I can do:
for i in range(1,11):
df[f'feature_{i}']=df['sepal_length'].shift(i)
Where df
is a pandas
Dataframe.
Any equivalent code for doing this in R
?
CodePudding user response:
An equivalent in R
tidyverse
is dplyr::lag
. Create the column in mutate
and update the object by assigning (<-
) back to the same object 'df'
library(dplyr)
df <- df %>%
mutate(shifted_x = lag(x))
or if we need to use the shift
, there is shift
in data.table
library(data.table)
setDT(df)[, shifted_x := shift(x)]
Also, if we need to create more than one column, shift
can take a vector of values in n
setDT(df)[, paste0('shifted', 1:3) := shift(x, n = 1:3)]
CodePudding user response:
We can also use the flag
function from the collapse
package.
library(collapse)
df %>%
ftransform(shifted_x = flag(x))
# # A tibble: 3 x 3
# x y shifted_x
# * <dbl> <dbl> <dbl>
# 1 1 4 NA
# 2 2 5 1
# 3 3 6 2