R:为数据帧的每个子集找到斜率

时间:2015-10-18 23:19:10

标签: r if-statement dataframe subset plyr

我有一个数据:

dat <- data.frame(ID=sample(1:10, 100, rep=T),
                  Date=seq(as.Date("1982/01/01"), by="16 days", length.out = 100),
                  Value1=runif(100))

我需要按年份和ID对数据进行子集化,并在Jan-Jun和Jul-Dec的数据中加上一行,并写出2个斜率系数,我需要对Year和ID的所有组合执行此操作。< / p>

除了循环之外,有没有办法做到这一点?实际数据有21788928行,循环时间太长。

1 个答案:

答案 0 :(得分:1)

这应该更快,但我不确定它是否足够快以满足您的需求。让我知道:

library(dplyr)
library(lubridate)

# Function to return the coefficients of the regression as a data frame
coef.fcn = function(df) {
  coeffs = coef(lm(Value1 ~ Date, data=df))
  return(data.frame(Intercept=coeffs[1], Value1=coeffs[2]))
}

lm_coefs = dat %>% 
  mutate(my.cat = ifelse(month(Date) %in% 1:6, 
                         paste("Jan-Jun", year(Date)), paste("Jul-Dec", year(Date)))) %>%
  group_by(ID, my.cat) %>%
  do(coef.fcn(.))

以下是您的样本数据的结果的一部分:

lm.coefs

   ID       my.cat    Intercept        Value1
1   1 Jan-Jun 1983   0.62824396            NA
2   1 Jan-Jun 1985   0.71865235            NA
3   1 Jul-Dec 1985  20.20901291 -0.0033972977
4   2 Jan-Jun 1983 -37.54324401  0.0078885381
...
45  8 Jan-Jun 1982 -30.39203349  0.0068229828
46  8 Jan-Jun 1984 -27.62517465  0.0054096259
47  8 Jan-Jun 1985  27.70049296 -0.0048539844
48  8 Jul-Dec 1982  12.90814643 -0.0025997511
49  8 Jul-Dec 1984 -16.84585961  0.0032229997
...
57 10 Jan-Jun 1982   0.63533344            NA
58 10 Jan-Jun 1983   0.35107513            NA
59 10 Jan-Jun 1984   0.59588750            NA
60 10 Jul-Dec 1982   0.05156481            NA
61 10 Jul-Dec 1983   0.54658810            NA