I am trying to calculate the difference between two dates in a data frame in days and save it in another column of the same data frame called lag. This is my code but it is not working:
Daily_Streamflow$Date
Daily_Streamflow$lag[1] <- 0
j <- 2
for (j in length(Daily_Streamflow$Date)) {
Daily_Streamflow$lag[j] <- as.numeric(difftime(Daily_Streamflow$Date[j],Daily_Streamflow$Date[j-1], unit=c("days")))
j <- j + 1
}
This is how my data frame looks:
When I set the difference for the first day Daily_Streamflow$lag[1]
equal to 0 all the remaining values are set to 0. If I keep this value empty, all the values of the column are set to NA
. It seems that the code put the value of the first lag to all the column, omitting the function inside the for, though the j value increases and the loop runs.