Lags in R within specific subsets?

六眼飞鱼酱① 提交于 2019-12-07 12:59:30

With data.table:

library(data.table)
setDT(df)[,`:=`(unemp_lag1=shift(unemp,n=1L,fill=NA, type="lag")),by=.(state, county)][]

   yearmonth state county unemp unemp_lag1
1:   2005-01     1      3   4.0         NA
2:   2005-02     1      3   3.6        4.0
3:   2005-03     1      3   1.4        3.6
4:   2005-01     2      3   3.7         NA
5:   2005-02     2      3   6.5        3.7
6:   2005-03     2      3   5.4        6.5

Just an old style base R approach:

dsp <- split(df, list(df$state, df$county) )
dsp <- lapply(dsp, function(x) transform(x, unemp_lag =lag(unemp)))
dsp <- unsplit(dsp, list(df$state, df$county))
dsp
yearmonth state county unemp unemp_lag
1   2005-01     1      3   4.0        NA
2   2005-02     1      3   3.6       4.0
3   2005-03     1      3   1.4       3.6
4   2005-01     2      3   3.7        NA
5   2005-02     2      3   6.5       3.7
6   2005-03     2      3   5.4       6.5

Edit

the lag function I used in my solution is the lag of dplyr (even though I didn't realized it until the BlondedDust comment) and here is a true and real pure base R solution (I hope):

dsp <- split(df, list(df$state, df$county) )
dsp <- lapply(dsp, function(x) transform(x, unemp_lag = c(NA, unemp[1:length(unemp)-1]) ) )
dsp <- unsplit(dsp, list(df$state, df$county))
dsp
  yearmonth state county unemp unemp_lag
1   2005-01     1      3   4.0        NA
2   2005-02     1      3   3.6       4.0
3   2005-03     1      3   1.4       3.6
4   2005-01     2      3   3.7        NA
5   2005-02     2      3   6.5       3.7
6   2005-03     2      3   5.4       6.5

With :

> library(dplyr)
> df %>% group_by(state, county) %>% mutate(unemp_lag=lag(unemp))
Source: local data frame [6 x 5]
Groups: state, county

   yearmonth state county unemp unemp_lag
1   2005-01     1      3   4.0        NA
2   2005-02     1      3   3.6       4.0
3   2005-03     1      3   1.4       3.6
4   2005-01     2      3   3.7        NA
5   2005-02     2      3   6.5       3.7
6   2005-03     2      3   5.4       6.5

And with :

> df2 <- as.data.table(df)
> df2[, unemp_lag := c(NA , unemp[-.N]), by=list(state, county)]

   yearmonth state county unemp unemp_lag
1:   2005-01     1      3   4.0        NA
2:   2005-02     1      3   3.6       4.0
3:   2005-03     1      3   1.4       3.6
4:   2005-01     2      3   3.7        NA
5:   2005-02     2      3   6.5       3.7
6:   2005-03     2      3   5.4       6.5
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!