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)