I have big data frame (dim: 12867779x5) which looks like that:
| id | group | date1 | date 2 | icf |
|---|---|---|---|---|
| id1 | 2 | 2020-03-17 | 2019-06-05 | |
| id1 | 3 | 2020-04-03 | 2019-05-09 | |
| id2 | 2 | 2020-04-10 | 2019-07-04 | |
| id2 | 3 | 2021-04-1 | 2020-06-01 | |
| id3 | 1 | 2020-04-13 | 2019-07-07 | |
| id3 | 2 | 2021-04-10 | 2020-06-01 | |
| id3 | 3 | 2020-04-10 | 2019-07-04 | |
| id3 | 4 | 2021-04-13 | 2020-06-01 |
Desired output:
| id | group | date1 | date 2 | icf |
|---|---|---|---|---|
| id1 | 3 | 2020-04-03 | 2019-05-09 | 0 |
| id2 | 2 | 2020-04-10 | 2019-07-04 | 52 |
| id2 | 3 | 2021-04-01 | 2020-06-01 | 0 |
| id3 | 1 | 2020-04-13 | 2019-07-07 | 49 |
| id3 | 2 | 2021-04-10 | 2020-06-01 | -646 |
| id3 | 3 | 2020-04-10 | 2019-07-04 | 52 |
| id3 | 4 | 2021-04-13 | 2020-06-01 | 0 |
To calculate icf I need to check if the id's from row i and i+1 are the same. If yes icf = date2(i+1) - date1(i).
I wrote this function to calculate icf, but it's too slow. I'm looking for ways to speed it up, I was thinking about using the apply function but I don't have idea how to re-write this icfCalculation fucntion.
icfCalculation <- function(dataFrame){
nr <- nrow(dataFrame) - 1
for (i in 1:nr) {
if(dataFrame[i, 1] == dataFrame[i+1, 1]){
dataFrame[i,5] = dataFrame[i+1, 4] - dataFrame[i, 3]
}
else{
dataFrame[i,5] = 0
}
}
return(dataFrame)
}