James
James

Reputation: 157

How to remove all rows in a data frame before a certain timestamp?

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

Answers (2)

Rodrigo Lustosa
Rodrigo Lustosa

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

Martin Gal
Martin Gal

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

Related Questions