Home > Net >  summarise_all with additional parameter that is a vector
summarise_all with additional parameter that is a vector

Time:03-12

Say I have a data frame:

df <- data.frame(a = 1:10, 
                 b = 1:10, 
                 c = 1:10)

I'd like to apply several summary functions to each column, so I use dplyr::summarise_all

library(dplyr)

df %>% summarise_all(.funs = c(mean, sum))
#   a_fn1 b_fn1 c_fn1 a_fn2 b_fn2 c_fn2
# 1   5.5   5.5   5.5    55    55    55

This works great! Now, say I have a function that takes an extra parameter. For example, this function calculates the number of elements in a column above a threshold. (Note: this is a toy example and not the real function.)

n_above_threshold <- function(x, threshold) sum(x > threshold)

So, the function works like this:

n_above_threshold(1:10, 5)
#[1] 5

I can apply it to all columns like before, but this time passing the additional parameter, like so:

df %>% summarise_all(.funs = c(mean, n_above_threshold), threshold = 5)
#   a_fn1 b_fn1 c_fn1 a_fn2 b_fn2 c_fn2
# 1   5.5   5.5   5.5     5     5     5

But, say I have a vector of thresholds where each element corresponds to a column. Say, c(1, 5, 7) for my example above. Of course, I can't simply do this, as it doesn't make any sense:

df %>% summarise_all(.funs = c(mean, n_above_threshold), threshold = c(1, 5, 7))

If I was using base R, I might do this:

> mapply(n_above_threshold, df, c(1, 5, 7))
# a b c 
# 9 5 3 

Is there a way of getting this result as part of a dplyr piped workflow like I was using for the simpler cases?

CodePudding user response:

dplyr provides a bunch of context-dependent functions. One is cur_column(). You can use it in summarise to look up the threshold for a given column.

library("tidyverse")

df <- data.frame(
  a = 1:10,
  b = 1:10,
  c = 1:10
)

n_above_threshold <- function(x, threshold) sum(x > threshold)

# Pair the parameters with the columns
thresholds <- c(1, 5, 7)
names(thresholds) <- colnames(df)

df %>%
  summarise(
    across(
      everything(),
      # Use `cur_column()` to access each column name in turn
      list(count = ~ n_above_threshold(.x, thresholds[cur_column()]),
           mean = mean)
    )
  )
#>   a_count a_mean b_count b_mean c_count c_mean
#> 1       9    5.5       5    5.5       3    5.5

This returns NA silently if the current column name doesn't have a known threshold. This is something that you might or might not want to happen.

df %>%
  # Add extra column to show what happens if we don't know the threshold for a column
  mutate(
    x = 1:10
  ) %>%
  summarise(
    across(
      everything(),
      # Use `cur_column()` to access each column name in turn
      list(count = ~ n_above_threshold(.x, thresholds[cur_column()]),
           mean = mean)
    )
  )
#>   a_count a_mean b_count b_mean c_count c_mean x_count x_mean
#> 1       9    5.5       5    5.5       3    5.5      NA    5.5

Created on 2022-03-11 by the reprex package (v2.0.1)

  • Related