'How to remove data based on the timestamp
I have a data frame that looks like this:
head(data1)
# A tibble: 6 × 10
Date Time Axis1 Axis2 Axis3 VM Standing Stepping Cycling
<date> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2022-03-17 11:29:00 0 0 0 0 0 0 0
2 2022-03-17 11:29:00 0 0 0 0 0 0 0
3 2022-03-17 11:29:00 0 0 0 0 0 0 0
4 2022-03-17 11:29:00 0 0 0 0 0 0 0
5 2022-03-17 11:29:00 0 0 0 0 0 0 0
6 2022-03-17 11:29:00 0 0 0 0 0 0 0
# … with 1 more variable: New_Sitting <dbl>
It has a data point every second all day, every day for a week. Is it possible to remove the data points outside of the workday? (Say only keep the data between 7am-5pm of the weekdays)
Solution 1:[1]
Use the following: Note that we first create a datetime object, then we filter whereby the weekday is between 1:5, and the time is between 07(7am) and 17(5pm)
library(tidyverse)
library(lubridate)
df %>%
mutate(date_time = ymd_hms(paste(Date, Time)))%>%
filter(format(date_time, "%u")%in%1:5,
date_time>=ymd_h(paste(Date, "07")),
date_time <= ymd_h(paste(Date, "17"))) %>%
select(-date_time)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
