Reputation: 798
I'm trying to lag a column, but the values disappear.
Example Data
df <- data.frame(x = c(1, 2, 3),
y = c(3, 5, 1),
z = c(2, NA, NA))
df
x y z
1 3 2
2 5 NA
3 1 NA
Current lag function
library(dplyr)
df |>
mutate(x = lag(x, 1))
x y z
NA 3 2
1 5 NA
2 1 NA
I would like for it to look like this:
Expected Output
x y z
NA 3 2
1 5 NA
2 1 NA
3 NA NA
Upvotes: 1
Views: 70
Reputation: 3414
dplyr
's lag
function cut the vector in the end so to fit the initial length. So lag
's input is 3-element vector and output 3-element vector in your example. Your idea is to have input 3-element and ouput 4-element. So you need to act on the structure of dataframe (number of rows) than on the level of individual columns, it is the area where lag
is working. Please see the code below, please pay attention that I did not checked for negative values, for how many to shift, multiple columns and so on.
library(dplyr)
df <- data.frame(x = c(1, 2, 3),
y = c(3, 5, 1),
z = c(2, NA, NA))
lag2 <- function(df, col_name, n) {
col_res <- c(rep(NA, n), df[, col_name])
temp <- df[1:n, ]
temp[] <- NA
df <- rbind(df, temp)
df[, col_name] <- col_res
df
}
df |> lag2("x", 1)
Output:
x y z
1 NA 3 2
2 1 5 NA
3 2 1 NA
4 3 NA NA
Upvotes: 1