Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 312
fiodeno Avatar asked May 06 '20 15:05

fiodeno


2 Answers

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(.)))
like image 113
tmfmnk Avatar answered Oct 24 '22 09:10

tmfmnk


We could use mutate_if with distinct

library(dplyr)
library(purrr)
df_finale %>%
     mutate_if(is.numeric, mean, na.rm = TRUE) %>% 
     mutate_if(negate(is.numeric), Mode) %>%
     distinct
#   num str
#1   4 toy

Or with across/summarise from the new version of dplyr

i1 <- df_finale %>% 
           summarise_all(is.numeric) %>%
           flatten_lgl

df_finale %>% 
     summarise(across(names(.)[i1], ~ mean(., na.rm = TRUE)), 
               across(names(.)[!i1], Mode))
like image 37
akrun Avatar answered Oct 24 '22 10:10

akrun