Reputation: 1408
If I have this data:
dat=structure(list(year = c(1980L, 1980L, 1980L, 1980L, 1980L, 1980L
), DOY = c(144, 144.042, 144.083, 144.125, 144.167, 144.208),
time = c(0, 1, 2, 3, 4, 5), B = c(67, 7, 7,
0, 7, 6), gg = c(67, 7, 7,
0, 7, 6)),row.names = c(NA, 6L), class = "data.frame")
I would like to compute the max and min per day of all columns and return
year DOY max.B min.B mean.B max.gg min.gg mean.gg
I have several columns not only B and gg
Upvotes: 0
Views: 339
Reputation: 76412
Maybe the followqing does what the question asks for.
library(dplyr)
library(tidyr)
dat %>%
mutate(day = strptime(paste(year, DOY), format = "%Y %j"),
day = as.Date(day)) %>%
select(-year, -DOY) %>%
pivot_longer(-day) %>%
group_by(day, name) %>%
summarise(max = max(value), min = min(value), .groups = "drop") %>%
pivot_wider(
id_cols = day,
names_from = name,
names_glue = "{name}_{.value}",
values_from = c(max, min)
)
To compute the means of min
and max
, assign the previous result to a variable daily
, keep only the year/month in day
, reshape to long format, group and compute the value
column means. Then reshape back to wide format.
daily %>%
mutate(month = format(day, "%Y-%m")) %>%
select(-day) %>%
pivot_longer(-month) %>%
mutate(name = sub("_.*$", "", name)) %>%
group_by(month, name) %>%
summarise(mean = mean(value, na.rm = TRUE), .groups = "drop") %>%
pivot_wider(
id_cols = month,
names_from = name,
names_glue = "{name}_{.value}",
values_from = mean
)
Upvotes: 1
Reputation: 3973
With data.table you can do:
require(data.table)
setDT(dat)
dat[ ,
.(min.B = min(B),
max.B = max(B),
mean.B = mean(B),
min.gg = min(gg),
max.gg = max(gg),
mean.gg = mean(gg)),
.(year, DOY) ]
Upvotes: 1