lag/lead entire dataframe in R

半世苍凉 提交于 2020-07-20 04:49:15

问题


I am having a very hard time leading or lagging an entire dataframe. What I am able to do is shifting individual columns with the following attempts but not the whole thing:

require('DataCombine')
df_l <- slide(df, Var = var1, slideBy = -1)

using colnames(x_ret_mon) as Var does not work, I am told the variable names are not found in the dataframe.

This attempt shifts the columns right but not down:

 df_l<- dplyr::lag(df)

This only creates new variables for the lagged variables but then I do not know how to effectively delete the old non lagged values:

 df_l<-shift(df, n=1L, fill=NA, type=c("lead"), give.names=FALSE)

回答1:


Use dplyr::mutate_all to apply lags or leads to all columns.

df = data.frame(a = 1:10, b = 21:30)
dplyr::mutate_all(df, lag)
    a  b
1  NA NA
2   1 21
3   2 22
4   3 23
5   4 24
6   5 25
7   6 26
8   7 27
9   8 28
10  9 29



回答2:


I don't see the point in lagging all columns in a data.frame. Wouldn't that just correspond to rbinding an NA row to your original data.frame (minus its last row)?

df = data.frame(a = 1:10, b = 21:30)
rbind(NA, df[-nrow(df), ]);
#    a  b
#1  NA NA
#2   1 21
#3   2 22
#4   3 23
#5   4 24
#6   5 25
#7   6 26
#8   7 27
#9   8 28
#10  9 29

And similarly for leading all columns.




回答3:


A couple more options

data.frame(lapply(df, lag))

require(purrr)
map_df(df, lag)

If your data is a data.table you can do

require(data.table)
as.data.table(shift(df))

Or, if you're overwriting df

df[] <- lapply(df, lag) # Thanks Moody
require(magrittr)
df %<>% map_df(lag)


来源:https://stackoverflow.com/questions/49437319/lag-lead-entire-dataframe-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!