R函数在管道中使用时不起作用



我创建了这个函数:

library(tibble); library(dplyr); library(magrittr)
ddes <- function(variable) {

n = length(variable)
median = median(variable)
mean = mean(variable)
sd = sd(variable)
se = round(sqrt(var(variable) / length(variable)), 2)
lower.95 = mean(variable) - (abs(qt(0.025, df = n - 1)) * se)
upper.95 = mean(variable) + (abs(qt(0.025, df = n - 1)) * se)

tibble(n, median, mean, sd, se, lower.95, upper.95)

}

它自己可以正常工作:

ddes(mtcars$mpg)
# # A tibble: 1 × 7
# n median  mean    sd    se lower.95 upper.95
# <int>  <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
#   1    32   19.2  20.1  6.03  1.07     17.9     22.3

但是在pipe中使用时失败。我如何使用管道?

mtcars %>% ddes(mpg)
# Error in ddes(., mpg) : unused argument (mpg)

%>%之后调用的函数应该在第一个位置有一个data参数(或另一个名称),这就是为什么您在错误消息中看到ddes(., mpg)。您可以这样重写函数:

library(tibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
ddes <- function(data, variable) {

mydata <- pull(data, {{ variable }})
n = length(mydata)
median = median(mydata)
mean = mean(mydata)
sd = sd(mydata)
se = round(sqrt(var(mydata) / length(mydata)), 2)
lower.95 = mean(mydata) - (abs(qt(0.025, df = n - 1)) * se)
upper.95 = mean(mydata) + (abs(qt(0.025, df = n - 1)) * se)

tibble(n, median, mean, sd, se, lower.95, upper.95)

}
mtcars %>% ddes(mpg)
#> # A tibble: 1 × 7
#>       n median  mean    sd    se lower.95 upper.95
#>   <int>  <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
#> 1    32   19.2  20.1  6.03  1.07     17.9     22.3

参见dplyr编程插图中的更多细节。

应该是

mtcars$mpg %>% ddes()

,因为管道操作符将整个变量替换为ddes函数的第一个参数。

另一种方法是使用大括号:

library(dplyr)
ddes <- function(df, varname) {

df %>% 
summarize(
n = n(),
median = median({{varname}}),
mean = mean({{varname}}),
sd = sd({{varname}}),
se = round(sd / sqrt(n), 2),
lower.95 = mean - (abs(qt(0.025, df = n - 1)) * se),
upper.95 = mean + (abs(qt(0.025, df = n - 1)) * se)
)

}
mtcars %>% 
ddes(mpg)
n median     mean       sd   se lower.95 upper.95
1 32   19.2 20.09062 6.026948 1.07 17.90835  22.2729

相关内容

  • 没有找到相关文章

最新更新