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)
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(.)))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With