I want to create lagged variable for a variable pm10 and used the following code. However, I could not get what I wanted. How could I create a lag of pm10?
df2$l1pm10 <- lag(df2$pm10, -1, na.pad = TRUE)
df2$l1pm102 <- lag(df2$pm10, 1)
dput(df2)
structure(list(var1 = 1:10, pm10 = c(26.956073733, NA, 32.838694951,
39.9560737332, NA, 40.9560737332, 33.956073733, 28.956073733,
32.348770798, NA), l1pm10 = structure(c(26.956073733, NA, 32.838694951,
39.9560737332, NA, 40.9560737332, 33.956073733, 28.956073733,
32.348770798, NA), .Tsp = c(2, 11, 1))), .Names = c("var1", "pm10",
"l1pm10"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10"), class = "data.frame")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…