在新创建的列中,使用上面的行中的值使用dplyr计算下一行

时间:2017-11-01 15:00:31

标签: r dplyr

假设我有一个如下所示的数据框:

library(dplyr)
library(tibble)
df <- tibble(t = 1:3, a = c(4, 6, 11), b = 1:3, c = c(1, 3, 2))

df
# A tibble: 3 x 4
#       t     a     b     c
#   <int> <dbl> <int> <dbl>
# 1     1     4     1     1
# 2     2     6     2     3
# 3     3    11     3     2

我想计算一个新列d,以便

  

d(0)= a

     

d(t)= d(t-1)+ b(t-1)+ c(t-1)

我可以通过for循环(痛苦地)做到这一点:

for_loop <- add_column(df, d = 0)
for (i in 1:nrow(df)) {
  if(i == 1) {
    for_loop[i,]$d <- for_loop[i,]$a
  }
  else {
    for_loop[i,]$d <- for_loop[i-1,]$d + for_loop[i-1,]$b + for_loop[i-1,]$c
  }
}
for_loop
# A tibble: 3 x 5
#       t     a     b     c     d
#   <int> <dbl> <int> <dbl> <dbl>
# 1     1     4     1     1     4
# 2     2     6     2     3     6
# 3     3    11     3     2    11

但我想用dplyr来做。尝试简单的ifelse似乎不起作用,因为计算是矢量化的,因此使用d的旧值:

tidy <- add_column(df, d = 0) %>%
  mutate(tidy, d = ifelse(row_number(d)==1, a, lag(d) + lag(b) + lag(c)))
tidy
# A tibble: 3 x 5
#       t     a     b     c     d
#   <int> <dbl> <int> <dbl> <dbl>
# 1     1     4     1     1     4
# 2     2     6     2     3     2
# 3     3    11     3     2     5

如何使用之前的d值使用dplyr计算d的新值?

1 个答案:

答案 0 :(得分:1)

您可以在滞后if (element instanceof Expense) { Log.d(YOUR_LOG_TAG,"The if condition not executed") Expense expense = (Expense) element; Intent intent = new Intent(MainActivity.this, ExpenseDetailActivity.class); intent.putExtra(ExpenseDetailActivity.EXPENSE_KEY, expense); startActivity(intent); } 上执行cumsum,然后将初始值b+c添加到结果中:

a[1]

为了对其进行矢量化,您需要从公式中删除之前的df %>% mutate(d = a[1] + cumsum(lag(b + c, default = 0))) # A tibble: 3 x 5 # t a b c d # <int> <dbl> <int> <dbl> <dbl> #1 1 4 1 1 4 #2 2 6 2 3 6 #3 3 11 3 2 11 d [t-1] ),如下所示:

d