I have a table like this :
and i would like to calculate the average of mean values for each column in table one for the same start and ends in order to have a table like this :
Can you tell me how to do this table in R ?
I have a table like this :
and i would like to calculate the average of mean values for each column in table one for the same start and ends in order to have a table like this :
Can you tell me how to do this table in R ?
A base solution with aggregate():
aggregate(. ~ chr + i.start + i.end + coverage_con, df, mean)
The dplyr version:
library(dplyr)
df %>%
group_by(chr, i.start, i.end, coverage_con) %>%
summarise(across(.fns = mean, .names = "average_{col}"))
summarise(across(.fns = mean)) is equivalent to summarise_all(mean), but the former can adjust column names by the glue specification.
If the data include other non-numeric columns except those grouped ones, you can calculate means only on those numeric columns by where(), i.e.
... %>%
summarise(across(where(is.numeric), mean, .names = "average_{col}"))
which is equivalent to summarise_if(is.numeric, mean).
Considering that your dataframe was called df, you can do:
library(dplyr)
df %>%
group_by(chr, i.start, i.end, coverage_con) %>%
summarize_all(mean)