使用R中年间隙的滞后函数计算变化

时间:2016-07-19 02:50:35

标签: r dplyr lag

我看到的所有滞后示例都使用连续时间序列。我试图按年计算百分比变化,但是,如果计算两者之间是否存在误差,对我来说是没有意义的。 也就是说,我不希望从2001年到2004年的百分比变化。只对两年之间感兴趣。数据输入示例:

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), 
    Year = c(2000L, 2001L, 2004L, 2005L, 2006L, 2007L, 1990L, 
    2000L, 2001L, 2005L, 2006L, 2007L, 2009L), Value = c(4L, 
    10L, 7L, 4L, 7L, 5L, 2L, 7L, 10L, 6L, 9L, 2L, 9L)), .Names = c("ID", 
"Year", "Value"), class = "data.frame", row.names = c(NA, -13L
))

df <-  df %>%  group_by(ID) %>%
  mutate(delta = (Value-lag(Value))/lag(Value))

上面的行不会返回我想要的输出,忽略跳转的位置。 期望的输出:

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), 
    Year = c(2000L, 2001L, 2004L, 2005L, 2006L, 2007L, 1990L, 
    2000L, 2001L, 2005L, 2006L, 2007L, 2009L), Value = c(4L, 
    10L, 7L, 4L, 7L, 5L, 2L, 7L, 10L, 6L, 9L, 2L, 9L), Change = c(NA, 
    1.5, NA, -0.428571429, 0.75, -0.285714286, NA, 2.5, 0.428571429, 
    NA, 0.5, -0.777777778, NA)), .Names = c("ID", "Year", "Value", 
"Change"), class = "data.frame", row.names = c(NA, -13L))

4 个答案:

答案 0 :(得分:5)

使用df %>% group_by(ID) %>% mutate(delta = ifelse((Year - lag(Year)) > 1, NA, (Value-lag(Value))/lag(Value)))

Type, Answer 1, Answer 2, Answer 3
Master, 100, 200, 300
Submission 1, 100, 400, 300
Submission 2, 100, 200, 300
Submission 3, 200, 100, 300

答案 1 :(得分:3)

以下是data.table解决方案:

# load library and convert to data.table
library(data.table)
setDT(df)

df[, "Change" := ifelse(Year-shift(Year)==1, 
   (Value-shift(Value))/shift(Value), NA), by="ID"]

返回

df
    ID Year Value     Change
 1:  A 2000     4         NA
 2:  A 2001    10  1.5000000
 3:  A 2004     7         NA
 4:  A 2005     4 -0.4285714
 5:  A 2006     7  0.7500000
 6:  A 2007     5 -0.2857143
 7:  B 1990     2         NA
 8:  B 2000     7         NA
 9:  B 2001    10  0.4285714
10:  B 2005     6         NA
11:  B 2006     9  0.5000000
12:  B 2007     2 -0.7777778
13:  B 2009     9         NA

这使用ifelse,对于庞大的数据集来说可能会很慢,但如果数据集的数量达到数千个,那么这将是不明显的。

答案 2 :(得分:3)

这是使用library(dplyr) df <- df %>% group_by(ID) %>% mutate(delta = (Value-lag(Value))/lag(Value)) #find the difference between each row yeardiff<-c(0,diff(df$Year) ) #for any row with a difference not equal to one set to NA df$delta[yeardiff !=1]<-NA 函数的可能解决方案。

resultado.rows

答案 3 :(得分:2)

我们可以使用base R函数来获取输出

lv <- with(df, ave(Value, ID, FUN = function(x) c(NA, x[-length(x)])))
ly <- with(df, ave(Year, ID, FUN = function(x) c(NA, x[-length(x)])))
df$Change <- with(df, ifelse((Year -ly) >1, NA, (Value - lv)/lv))
df$Change
#[1]         NA  1.5000000         NA -0.4285714  0.7500000 
#[6] -0.2857143         NA         NA  0.4285714         NA  
#[11] 0.5000000 -0.7777778         NA