rtime-series

Delete a single value in dataframe based on particular date and time


I have a dataframe and I want to delete a single value based on the date and time, I want to delete the value for Variable 2 on the 2020-06-15 14:00:00 with an option to replace the value 780.45 with NA, or to leave it blank. I can find answers where the row is deleted based on the datetime but struggling to find where a single value is deleted.

df2 = structure(list(DateTime = structure(c(1592226000, 1592226900, 
1592227800, 1592228700, 1592229600, 1592230500), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Variable1 = c(NA, 0.385999999999999, 0.193, 
0.290000000000001, 0.385, 0.576000000000001), Variable2 = c(NA, 1005.87, 
999.05, 1005.32, 780.45, 1100.44)), row.names = c(NA, 6L), 
class = "data.frame")

Created on 2024-07-05 with reprex v2.1.0


Solution

  • The "tricky" part is to get the date-time in the right format without time zone. One way is to use format.Date

    df2$Variable2[format.Date(df2$DateTime, "%F %T") == "2020-06-15 14:00:00"] <- NA
    

    Output

    df2
                 DateTime Variable1 Variable2
    1 2020-06-15 13:00:00        NA        NA
    2 2020-06-15 13:15:00     0.386   1005.87
    3 2020-06-15 13:30:00     0.193    999.05
    4 2020-06-15 13:45:00     0.290   1005.32
    5 2020-06-15 14:00:00     0.385        NA
    6 2020-06-15 14:15:00     0.576   1100.44