简体   繁体   English

计算一个 data.frame 中某些行的总和

[英]Calculating sum of certain rows within one data.frame

I have a data.frame called "test" like this:我有一个名为“test”的data.frame如下所示:

   Age START_DATE   END_DATE ACCT_NO PRINCIPAL_AMOUNT_BASE
1   60 01/05/2014 30/06/2014    ACC1                   400
2  121 01/03/2014 30/06/2014    ACC2                   200
3  121 01/03/2014 30/06/2014    ACC3                   300
4  180 01/01/2014 30/06/2014    ACC4                   100
5  183 01/07/2014 31/12/2014    ACC5                   200
6  914 01/07/2014 31/12/2016    ACC6                   300
7  914 01/07/2014 31/12/2016    ACC7                   500
8 1644 01/07/2014 31/12/2018    ACC8                    50

I'm trying to get the list of sum for PRINCIPAL_AMOUNT_BASE of between each account that have start date = a previous account that have end date + 1. For example: acc1 has end date of 30/06/2014 and acc5 has start date of 01/07/2014 => 400 + 200 = 600. Also each row's start date can only be used once (the next sum will be between acc2 and acc6, not acc2 and acc5).我正在尝试获取具有开始日期的每个帐户之间的PRINCIPAL_AMOUNT_BASE的总和列表 = 具有结束日期的前一个帐户 + 1。例如:acc1 的结束日期为 30/06/2014,acc5 的开始日期为01/07/2014 => 400 + 200 = 600。此外,每行的开始日期只能使用一次(下一个总和将在 acc2 和 acc6 之间,而不是 acc2 和 acc5 之间)。

Here is my code:这是我的代码:

visited <- vector()
num_list <-vector()

for (i in 1:nrow(test)){
  for (z in i+1:nrow(test)){
    if ((test[i, 3] + 1) == test[z,2]){
        if (z %in% visited){
          next
        } else {
          result <- test[i,5] + test[z,5]
          num_list <- c(num_list, result)
          visited <- c(visited, z)
          print (result)
          break
        }
    }
  } 
}

I'm getting this error:我收到此错误:

Error in if ((test[i, 3] + 1) == test[z, 2]) { : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In Ops.factor(test[i, 3], 1) : ‘+’ not meaningful for factors

What I'm expecting is a vector that contain the following number: (600, 500, 800, 150)我期待的是一个包含以下数字的向量: (600, 500, 800, 150)

Both loops for i and z will run for same iteration. i 和 z 的两个循环都将运行相同的迭代。 if date difference equals 1 then the result will be stored.如果日期差等于 1,则将存储结果。

visited <- vector()
num_list <-vector()

for (i in 1:nrow(test)){
  for (z in 1:nrow(test)){
    if (test[z,2] - test[i,3] == 1){
      if (z %in% visited){
        next
      } else {
        result <- test[i,5] + test[z,5]
        num_list <- c(num_list, result)
        visited <- c(visited, z)
        print (result)
        break
      }
    }
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM