# calculate summary for more than one column

I am trying to calculate median mean for group of columns but its calculating only for one column. what i am doing wrong here ...??

``````df <- data.frame(Name = c("ABC",	"DCA",	"GOL",NA,	"MNA",NA,	"VAN"),
Goal =c("published",	"pending",	"not designed",NA,	"pending",	"pending",	"not designed"),
Target_1 = c(3734,	2639,	2604,	NA,	2793,	2688,	2403),
Target_2 = c(3322,	2016,	2310,	NA,	3236,	3898,	2309),
Target_3 = c(3785,	2585,	3750,	NA,	2781,	3589,	2830))

df_summary <- df %>% select(contains("Target")) %>% summarise(
q25 = round(quantile(.,  type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[2],digits = 0),
Median = round(quantile(., type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[3],digits = 0),
Mean = round( mean(., na.rm=TRUE),digits = 0),
q75 = round(quantile(., type=6, probs = seq(0, 1, 0.25), na.rm=TRUE)[4],digits = 0),
N = sum(!is.na(.)))

``````

Don't use ".", use column name or tidyselect.

and Use summarise(across()) for the function.

In the past, summarise_*() was used, but now it is recommended to use summarise(across()).

``````df %>%
summarise(across(contains("Target"),median,na.rm=TRUE))
``````

return is

``````  Target_1 Target_2 Target_3
1   2663.5     2773   3209.5
``````
``````df %>%
summarise(across(contains("Target"),list(mean = mean, sd = sd),na.rm=TRUE))
``````
``````  Target_1_mean Target_1_sd Target_2_mean Target_2_sd Target_3_mean Target_3_sd
1      2810.167    470.3775        2848.5    741.6074          3220    544.8545
``````

Similar results can be obtained by using skim

``````df %>%
skimr::skim() %>%
as_tibble() %>%
filter(str_detect(skim_variable,"Target")) %>%
select(starts_with("numeric"))
``````

return is

``````  numeric.mean numeric.sd numeric.p0 numeric.p25 numeric.p50 numeric.p75 numeric.p100
<dbl>      <dbl>      <dbl>       <dbl>       <dbl>       <dbl>        <dbl>
1        2810.       470.       2403       2613.       2664.       2767.         3734
2        2848.       742.       2016       2309.       2773        3300.         3898
3        3220        545.       2585       2793.       3210.       3710.         3785
``````

I'd be happy to help you out.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.