Reputation: 157
I have a dataeframe and the first column is "date" in the format YYYY-MM-DD HH:MM:SS
.
I want to be able to remove all rows in the data frame that are prior to a certain date and time?
So for example, if below I want to remove all rows before 2021-07-19 11:30:30. How do I go about this?
The time I want to filer is also saved to the environment as a variable known as "startdatetime"
date
2021-07-19 11:30:00
2021-07-19 11:30:15
2021-07-19 11:30:30
2021-07-19 11:30:45
2021-07-19 11:31:00
Upvotes: 1
Views: 2156
Reputation: 76
I would use the package lubridate
together with dplyr
. lubridate
is designed to make operations with date and time.
Reproducing your example:
library(lubridate)
library(dplyr)
dates <- c("2021-07-19 11:30:00","2021-07-19 11:30:15","2021-07-19 11:30:30",
"2021-07-19 11:30:45","2021-07-19 11:31:00")
#coerce string to Date and Time
dates <- ymd_hms(dates) #which means year, month, day, hour, minute and second
df <- data.frame(date = dates)
df %>% filter(date >= ymd_hms("2021-07-19 11:30:30"))
Upvotes: 1
Reputation: 16988
You could use
library(dplyr)
df %>%
# filter(date >= as.POSIXct("2021-07-19 11:30:30"))
filter(date >= "2021-07-19 11:30:30") # thanks to ThomasIsCoding: as.POSIXct isn't necessary
which returns
date
1 2021-07-19 11:30:30
2 2021-07-19 11:30:45
3 2021-07-19 11:31:00
Upvotes: 3