I have pulled some data via sql into a dataframe. I am now trying to subset such data and have had no luck.
I wish to loop through each row and identify the previous hour, after which I wish to select a subset of the DF where date == previous hour. (I understand there are other ways of doing this however i wish to understand why this isn't working). When I do this it returns an empty df. However If i directly paste the value of previous hour as a string I get the result I desire.
Both variables are POSIXCT and any attempt to convert to character fails. Can someone please tell me what on earth is going on? :S
My code:
for(row in 1:3){
  PreviousHour <- as.POSIXct(Data$mydate[row] - hours(1), tz = "UTC")
  Date <- Data$mydate[row]
  print(c(Data$mydate[row],PreviousHour))
  #"2019-11-20 23:00:00 GMT" "2019-11-20 22:00:00 GMT"
  print(Data$mydate[row] == PreviousHour)
  #FALSE
  print(subset(Data,Data$mydate == PreviousHour))
  # A tibble 0x5
  print(subset(Data,Data$mydate == "2019-11-20 22:00:00 GMT"))
  # A tibble 1x5
}
Code if I manually create the df (This works):
mydate <- c(as.POSIXct("2019-11-20 22:00:00", tz = "UTC"),as.POSIXct("2019-11-20 21:00:00", tz = "UTC"))
Data <- data.frame(mydate)
for(row in 1:1){
  PreviousHour <- as.POSIXct(Data$mydate[row] - hours(1), tz = "UTC")
  Date <- Data$mydate[row]
  print(c(Data$mydate[row],PreviousHour))
  #"2019-11-20 22:00:00 GMT" "2019-11-20 21:00:00 GMT"
  print(Data$mydate[row] == PreviousHour)
  #FALSE
  print(subset(Data,Data$mydate == PreviousHour))
  # A tibble 1x1
}
