Specific Aggregate Type for Each Column - na.rm

Continuing the discussion from Specific Aggregate Type for Each Column:

Hi Nir,

One detail I forget to ask about this post, the aggregation functions should have an argument for na.rm = T or F. I generally default to T but would like to know where I could insert this argument in the code you provided?

Mike

I'll leave you with the same exercise to convert hand written code to computer written code; but the general answer is to either use an inline anonymous function call to set additional parameters, or explicitly make your own function with the desired defaults.

library(tidyverse)

# example data preperation
(hiris <- head(iris))
hiris[1,1]<-NA
hiris

# the defaults with NA :(
hiris  |>
  summarise(across(starts_with("Sepal"),
                   list(mean=mean)))
# Sepal.Length_mean Sepal.Width_mean
# 1                NA         3.383333

# inline with anonymouse function
hiris  |>
  summarise(across(starts_with("Sepal"),
                   list(mean=\(x)mean(x,
                                      na.rm=TRUE))))
# Sepal.Length_mean Sepal.Width_mean
# 1              4.92         3.383333


#use purrr's partial to make your own function

mymean <- purrr::partial(mean,na.rm=TRUE)
hiris  |>
  summarise(across(starts_with("Sepal"),
                   list(mean=mymean)))
# Sepal.Length_mean Sepal.Width_mean
# 1              4.92         3.383333

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.