1

I'd like to use my own function smd in summarize_at, without success. If I try to do:

library(dplyr)

# My function
smd<-function(x,...)
  {sd(x)/sqrt(length(x)-1)}

starwars %>%
  summarise_at(c("height", "mass"), smd, na.rm = TRUE)

Erro: C stack usage  15924224 is too close to the limit

Doesn't work!! Try to make funs(smd)and funs(sd/sqrt(n()-1)) and dosen't work too!

Please, any ideas?

r2evans
  • 141,215
  • 6
  • 77
  • 149
Leprechault
  • 1,531
  • 12
  • 28

2 Answers2

2

First change is to pass na.rm= on to sd(.), so

smd <- function(x, ...) sd(x, ...)/sqrt(length(x)-1)
starwars %>%
  summarise_at(c("height", "mass"), smd, na.rm=TRUE)
# # A tibble: 1 x 2
#   height  mass
#    <dbl> <dbl>
# 1   3.75  18.3

As @astrofunkswag suggested, though, you need to consider if NA values should decrease your length. For that, we need to replace length(x) with sum(!is.na(x)).

smd <- function(x, ...) sd(x, ...)/sqrt(sum(!is.na(x))-1)
starwars %>%
  summarise_at(c("height", "mass"), smd, na.rm=TRUE)
# # A tibble: 1 x 2
#   height  mass
#    <dbl> <dbl>
# 1   3.89  22.3
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Why @r2evans if I use `group_by` in `starwars%>% group_by(gender) %>% summarise_at(c("height", "mass"), smd, na.rm=TRUE)` doesn't work? Error: Problem with `summarise()` input `height`. x unused argument (na.rm = TRUE) i Input `height` is `(function (x, ...) ...`. i The error occurred in group 1: gender = "feminine". Run `rlang::last_error()` to see where the error occurred. – Leprechault Oct 08 '20 at 22:15
1

We can also do this with summarise/across

smd <- function(x, ...) sd(x, ...)/sqrt(sum(complete.cases(x))-1)
starwars %>%
     summarise(across(c(height, mass), smd, na.rm = TRUE))

-output

# A tibble: 1 x 2
#  height  mass
#   <dbl> <dbl>
#1   3.89  22.3


  
akrun
  • 874,273
  • 37
  • 540
  • 662