Antonio Mastroianni
Antonio Mastroianni

Reputation: 45

Filtering data by datetime variable R

Let's say I have the following dataframe on which I create a DateTime column:

df <- read.table(text="
Date         Time   pH
1976-01-26   0:00   4.00
1976-01-26   0:15   4.05
1976-01-26   1:00   4.50
1976-01-26   1:15   4.50
1976-01-27   0:00   4.00
1976-01-27   0:15   4.50
1076-01-27   2:00   4.00", header=T)

df$DateTime <- as.POSIXct(paste(df$Date, df$Time))

Hence, my data is the following:

        Date Time   pH            DateTime
1 1976-01-26 0:00 4.00 1976-01-26 00:00:00
2 1976-01-26 0:15 4.05 1976-01-26 00:15:00
3 1976-01-26 1:00 4.50 1976-01-26 01:00:00
4 1976-01-26 1:15 4.50 1976-01-26 01:15:00
5 1976-01-27 0:00 4.00 1976-01-27 00:00:00
6 1976-01-27 0:15 4.20 1976-01-27 00:15:00
7 1076-01-27 2:00 3.80 1076-01-27 02:00:00

How do I filter according to the DateTime column only the rows for which the minutes are 00:00? For example 00:00:00, 01:00:00, 02:00:00 have to be in the output

Upvotes: 1

Views: 779

Answers (2)

benson23
benson23

Reputation: 19097

Since you are handling date, I believe Ben's solution is more suitable for you. My approach is for general string matching.

Here we use grepl() to find pattern and return a logical value for filter().

UPDATED: Since you have updated your question, I have also updated my code.

df %>% as_tibble() %>% filter(grepl("0.:00:00", DateTime))

Output

# A tibble: 4 x 4
  Date       Time     pH DateTime           
  <chr>      <chr> <dbl> <dttm>             
1 1976-01-26 0:00    4   1976-01-26 00:00:00
2 1976-01-26 1:00    4.5 1976-01-26 01:00:00
3 1976-01-27 0:00    4   1976-01-27 00:00:00
4 1076-01-27 2:00    4   1076-01-27 02:00:00

Upvotes: 1

Ben
Ben

Reputation: 30474

If you want to filter where the time is '00:00:00' you could use as_hms to consider just time of day for filtering:

library(dplyr)
library(hms)

filter(df, as_hms(DateTime) == as_hms('00:00:00'))

To filter even hours (where number of minutes is zero), you can use minute from lubridate:

library(lubridate)

filter(df, minute(DateTime) == 0)

Likewise, if you want to filter on both zero minutes and seconds, you could do:

filter(df, minute(DateTime) == 0 & second(DateTime) == 0)

Upvotes: 1

Related Questions