I am trying to create an new column in a dataframe titled "Date2" which I will have to loop through previous iterations of the calculation.
The following is the logic.
If ID2 = Previous(ID2) then Previous(Date2) ELSE
Date1 + 60
Example:
ID2 Date 1 Date 2
1001 1/1/2019 3/2/2019
1001 1/1/2019 3/2/2019
In row 1 Date2 is simply Date1+60 since the previous ID2 is different.
In row 2 Date2 is simply the previous Date2 since the previous ID2 is the same as the current.
Code:
OrderedData$Well2BookDate <- 0 # initialize, sometimes better as just `list()`
for(i in 2:nrow(OrderedData)){
if(OrderedData[i,1]== OrderedData[i-1, 1]){
# Previous Well 2 Book Date
OrderedData[i,14 ] <- OrderedData[i-1, 14]
} else {
# Online Date 1 + 60 Days
OrderedData[i, 14] <- OrderedData[i, 9] + 60
}
}
This code works mostly except the Well 2 Book Date is being output as a number instead of a date. Online Date 1 was already a date class.