I have a dataframe with one date column and several columns with values (measured concentrations). I am mutating the dataframe and summarizing to years with averages of the values. This works fine:
library(dplyr)
df <- data.frame(mydate=as.Date(c("2000-01-15", "2000-02-15", "2000-03-15")), columnA=c(2, 4, 5), columnB=c(3, 6, 7))
df_year <- df %>%
mutate(year = format(mydate, format="%Y")) %>%
group_by(year) %>%
summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE)))
The above code gives me averages. Is it possible to use ifelse and have df_year summarized with n-percentile only in columns with a name contains "B"? That is, columnA will still summarize into average, but columnB will summarize into a percentile.
I know how to compute quantiles, but I'm not able to use ifelse in an efficient way. I don't want to create a new dataframe since it contains multiple columns that are later looped through when plotting. I am using grepl to catch the "B", but get error 'unused argument'. I'm looking for something like:
mutate...%>%
group_by...%>%
ifelse(grepl("B", each_column_name, fixed=TRUE)==TRUE,
summarise(across(where(is.numeric), ~ quantile(.x, probs=0.9, na.rm = TRUE))),
summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE))))