Search code examples
rdplyrmodereductionsummarize

r summarize_if with multiple conditions


I'm trying to reduce a df of observations to a single observation (single line). I would like to summarize_if is numeric with the mean and if is string or factor with the mode. The code below doesn't work, but I hope it gives the idea. Thanks!

#data frame
num <- c(1:7)
str <- c("toy","control","play",NA,"give","toy","toy")
df_finale <- data.frame(num,str)

#mode function
Mode <- function(x) {
        ux <- unique(x)
        ux[which.max(tabulate(match(x, ux)))]
}

#df reduction
df_finale <- df_finale %>%
                    summarize_if(is.numeric, mean, na.rm = TRUE) %>%
                    summarize_else_if(!is.numeric, Mode)

Solution

  • One possibility could be:

    df_finale %>%
     summarise_all(~ if(is.numeric(.)) mean(., na.rm = TRUE) else Mode(.))
    
      num str
    1   4 toy
    

    Or an option since dplyr 1.0.0:

    df_finale %>%
     summarise(across(everything(), ~ if(is.numeric(.)) mean(., na.rm = TRUE) else Mode(.)))