简体   繁体   中英

Calculating sum of certain rows within one data.frame

I have a data.frame called "test" like this:

   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).

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)

Both loops for i and z will run for same iteration. if date difference equals 1 then the result will be stored.

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
      }
    }
  } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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