I am trying to do an analysis on weather events from 2016 to 2021. So I want to convert this time period from 2016 to 2021 to days such that StartTime[1] = 2016-01-06 23:14:00 is day 1, all the way to StartTime[length(df$StartTime)] = 2021-12-28 12:18:00 is day 1822 (5*365 - 3).
I've converted the StartTime and EndTimes to POSIXct types and I tried to write a function to iterate over the dataframe to run difftime(StartTime[i], StartTime[1], units="days")
The time data comes in this format StartTime[1] = 2016-01-06 23:14:00
whereas StartTime[length(df$StartTime)] = 2021-12-28 12:18:00
This is the function I tried to write to do this..
cont_days<-function(date) {
for (i in df)
days<-difftime(date[i], date[1], units="days")
return(days)
}
df$continous_days<-cont_days(df$StartTime.UTC.)
but I'm getting this error message that I've been scratching my head over.
Error in `$<-.data.frame`(`*tmp*`, continous_days, value = c(NA_real_, :
replacement has 2453 rows, data has 2455
Edit: Okay so I did df<-na.omit(df) to ensure I removed all the NA rows.