I have a dataframe with datetime stamp in column one and data in column two. I want to filter or gather data for only specific date. For example from the below data, I want to gather data only for 05/10/2022, 05/12/2022 and 07/10/2022. Can someone guide me towards the best possible solution for this problem? Thanks.
structure(list(DateTime = structure(c(1652178480, 1652178540,
1652178600, 1652265060, 1652265120, 1652351580, 1652351640, 1652351700,
1652438160, 1652438220, 1652438280, 1652438340, 1652524800, 1652524860,
1652524920, 1652524980, 1652611440, 1652611500, 1657449960, 1657450020,
1657450080, 1657450140, 1657450200, 1657536660, 1657536720), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), data = c(1, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -25L))
CodePudding user response:
df %>%
filter(as.Date(DateTime) %in% as.Date(c("05/10/2022",
"05/12/2022",
"07/10/2022"),
format = "%m/%d/%Y"))
# A tibble: 11 x 2
DateTime data
<dttm> <dbl>
1 2022-05-10 10:28:00 1
2 2022-05-10 10:29:00 1
3 2022-05-10 10:30:00 0
4 2022-05-12 10:33:00 1
5 2022-05-12 10:34:00 0
6 2022-05-12 10:35:00 1
7 2022-07-10 10:46:00 0
8 2022-07-10 10:47:00 1
9 2022-07-10 10:48:00 1
10 2022-07-10 10:49:00 1
11 2022-07-10 10:50:00 0