I have a hourly data.frame Data
with a $date
column from years 2015 to 2021. The dates are in "%d%m%Y HH:MM"
format.
I need to remove specific rows with a certain date, e.g. remove all rows with date "01/09/2021".
I tried this:
Data <- Data[as.Date(format(Data$date, "%d%m%Y")) != 01/09/2021, ]
but this didn´t work. What's the correct way to do it? Thanks in advance for your help.
CodePudding user response:
The date-time conversion codes are listed in ?strptime
.
as.Date
returns a string of type "2021-09-01" and class Date
(see ?Dates
for details), so this would be the correct logic:
Data <- data.frame(id = 1:2, date = c("01012021 00:00","01092021 00:00"))
Data
#> id date
#> 1 1 01012021 00:00
#> 2 2 01092021 00:00
Data[as.Date(Data$date, format = "%d%m%Y") != "2021-09-01", ]
#> id date
#> 1 1 01012021 00:00
Note that strings such as "01/09/2021"
or "2021-09-01"
have to be quoted in R.
Created on 2021-09-21 by the reprex package (v2.0.1)
Does this help?
CodePudding user response:
You can use regex approach to remove rows that start with '01092021'
.
Using data from @scrameri
result <- subset(Data, !grepl('^01092021', date))
result
# id date
#1 1 01012021 00:00