Equivalent of n() or count() that will work .fns = list(Count = )

Hi, I have a following problem:

I use this command to compute mean and sum of purchase_amount column.

data %>% select(purchase_month, purchase_amount) %>%  group_by(purchase_month) %>%  
  summarise(across(
    .cols = is.numeric, 
    .fns = list(Mean = mean, Sum = sum) , 
    .names = "{col}_{fn}"
  ))

However, I also need to compute the total number of purchases in purchase_month.
I tried this:

 .fns = list(Mean = mean, Sum = sum, Count = n() ) 

but it does not work. I also tried Count = count, Count = .N

Can you help me please? Thanks a lot.

because you want functions that operate on vectors I think length() works .

iris %>%  group_by(Species) %>%  
  summarise(across(where(is.numeric), 
                   list(Mean = mean,
                Sum = sum,
                Count = length) , 
    .names = "{col}_{fn}"
  ))

This topic was automatically closed 21 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.