Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用dplyr对组或因子使用函数_R_Dplyr_Purrr_Magrittr - Fatal编程技术网

使用dplyr对组或因子使用函数

使用dplyr对组或因子使用函数,r,dplyr,purrr,magrittr,R,Dplyr,Purrr,Magrittr,我想在数据集中的几个组上使用一个函数,例如shapiro.test() 首先我试过 库(tidyverse) 图书馆(magrittr) mtcars%>%分组依据(气缸)%$%shapiro.test(wt)$p.value #> [1] 0.09265499 但这并没有像我预期的那样在这些组中迭代。 然后我尝试了一个函数,将结果输出为数据帧,因为这是这里关于堆栈溢出的另一个问题所采用的方法 checkNorm <- function(x) { return(data.frame(

我想在数据集中的几个组上使用一个函数,例如
shapiro.test()

首先我试过

库(tidyverse)
图书馆(magrittr)
mtcars%>%分组依据(气缸)%$%shapiro.test(wt)$p.value
#> [1] 0.09265499
但这并没有像我预期的那样在这些组中迭代。 然后我尝试了一个函数,将结果输出为数据帧,因为这是这里关于堆栈溢出的另一个问题所采用的方法

checkNorm <- function(x) {
  return(data.frame(P = shapiro.test(x)$p.value))
}

mtcars %>% group_by(cyl) %$% checkNorm(wt)
#>            P
#> 1 0.09265499
checkNorm%groupby(cyl)%%$%checkNorm(wt)
#>P
#> 1 0.09265499

使函数迭代通过
group\u by()
传递的组的适当方法是什么?

创建一个新列来存储每个组的p值:

library(dplyr)

mtcars %>% 
  group_by(cyl) %>%
  summarise(p_val = shapiro.test(wt)$p.value)

#   cyl   p_val
#  <dbl>   <dbl>
#1     4 0.570  
#2     6 0.131  
#3     8 0.00275
库(dplyr)
mtcars%>%
组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别组别
总结(p_val=shapiro.测试(wt)$p.值)
#共青团
#     
#1     4 0.570  
#2     6 0.131  
#3     8 0.00275

我完全忘记了
summary()
,这正是我想要的!谢谢你提醒我。