Home > Enterprise >  R : Is it possible to use median and na.rm together in summarise_all?
R : Is it possible to use median and na.rm together in summarise_all?

Time:12-02

I am trying to do 'summarise_all' for all columns using median and want to ignore null data using na.rm = T.

For example,

df_sector <- df %>% 
    group_by(Sector) %>% 
    summarise_all(mean, na.rm = T)

is working.

However,

df_sector <- df %>% 
    group_by(Sector) %>% 
    summarise_all(median, na.rm = T)

is not working.

Is there any way to summarise all columns using median and na.rm?

CodePudding user response:

Instead of summarize_all try across

df %>% 
  group_by(Sector) %>% 
  summarise(across(
    .cols = everything(),
    .fns =  list(mean = ~mean(.,na = TRUE), median = ~median(.,,na = TRUE))))
  • Related