如何对列的部分求和?

时间:2018-04-02 13:25:36

标签: r sum

我正在做一个关于为15分钟流量数据计算Flashiness索引的项目。 我有关于如何计算流数据的代码。

# new variable for lag time
flow_lagged_S <- S %>% mutate(
flow_lag = lag(flow, n = 1), #1st claculate lag 
Qi_Qi1 = abs(flow - flow_lag))# calculate the abs value of the diff
# calculate sums following the formula
RB_index_S <- flow_lagged_S %>%
summarise(RB_index = sum(,Qi_Qi1, na.rm = T) / sum(flow, na.rm = T))

这些数据是针对不同年份的,目前我可以计算整个电台的闪光但不是永久性的。 对于代码的最后一位,我需要更改它,以便计算每年的总和。我怎么做?因此,我需要在2002年将Qi_Qi1与Qi_Qi1相加而不是整列。 所以我的表flow_lagged_S看起来像这样:

time_stamp           flow    year  flow_lag  Qi_Qi1
2002-10-24 22:45:00   9.50   2002   N/A       N/a
2002-10-24 23:00:00  10.00   2002   9.50      0.50
2002-10-24 23:15:00   10.50   2002   10.00    0.50
2002-10-24 23:30:00  11.00   2002   10.50     0.70

1 个答案:

答案 0 :(得分:0)

您可以使用dplyr包中的group_by()函数:

df <- data.frame(time_stamp = c("2002-10-24 22:45:00", "2002-10-24 23:00:00", "2002-10-24 23:15:00", "2002-10-24 23:30:00"),
    flow = c(9.5, 10, 10.5, 11),
    year = c(2002, 2002, 2002, 2002), 
    flow_lag = c(NA, 9.5, 10, 10.5), 
    Qi_Qi = c(NA, .5, .5, .7))

df %>%
    group_by(year) %>%
    summarize(total = sum(Qi_Qi, na.rm = T))

答案是:

# A tibble: 1 x 2
   year total
  <dbl> <dbl>
1  2002   1.7