I'm still a little unclear on what exactly you need - so here are a few general methods that I think would get you close to what you are trying to do? I am using this as a dummy dataset to stand in for your real data:
library(dplyr)
library(lubridate)
library(purrr)
# Generating some timestamps
ts <- expand.grid(
2021,
1:6,
1:6,
1:6,
1:3,
1:2
) %>%
rowwise() %>%
mutate(
timestamp = paste0(
Var1,
'-',
Var2,
'-',
Var3,
' ',
Var4,
':',
Var5,
':',
Var6
) %>%
ymd_hms
) %>%
pull(timestamp)
# Some dummy data
df <- data.frame(
'timestamp' = ymd_hms(ts),
replicate(10, runif(length(ts)))
)
1. Extract the day, then split on day, apply functions, and combine.
df %>%
mutate(
dt_by_day = ISOdate(
year = year(timestamp),
month = month(timestamp),
day = day(timestamp)
)
) %>%
select(-timestamp) %>%
group_by(dt_by_day) %>%
summarize(
across(
everything(),
list(
mean = mean,
sd = sd
)
)
)
If you need to do something with more side effectrs, then you might want to look at dplyr::group_split. For example, this would write out data summaries for each Friday. There might be easier ways to do this, but without a sample of your data this would at least work.
df %>%
mutate(
timestamp = ISOdate(
year = year(timestamp),
month = month(timestamp),
day = day(timestamp)
)
) %>%
group_split(timestamp) %>%
map(
.f = function(df) {
dt <- df$timestamp[1]
day_of_week <- weekdays(dt)
if(day_of_week == 'Friday') {
tmp <- df %>%
summarize(
across(
where(is.numeric),
.fns = list(
mean = mean,
sd = sd
)
)
)
write.csv(tmp, paste0('data_', dt, '.csv'))
}
}
)