在 R 中执行 shapiro_test 时 'group_by' 函数出现问题

Issue with 'group_by' function when doing shapiro_test in R

我之前问过这个问题,但没有成功,所以再说一遍:

我的数据框:

data.type <- c("DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","DNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA","RNA")
hour <- c(1,1,1,2,2,2,24,24,24,48,48,48,96,96,96,168,168,168,672,672,672,1,1,1,2,2,2,24,24,24,48,48,48,96,96,96,168,168,168,672,672,672)
zotu.count <- c(11,14,16,7,16,15,5,14,13,6,5,17,7,7,12,3,4,5,3,5,4,2,3,2,1,6,2,1,1,1,1,0,0,1,1,4,1,1,1,6,7,6)
id <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42)

我正在尝试使用以下代码进行 shapiro 测试以测试我的数据是否正常,但出现以下错误:

dataset %>% group_by(data.type, hour) %>% shapiro_test(zotu.count)

Error: Problem with `mutate()` column `data`.
ℹ `data = map(.data$data, .f, ...)`.
x Problem with `mutate()` column `data`.
ℹ `data = map(.data$data, .f, ...)`.
x all 'x' values are identical

这很奇怪,因为它以前曾在具有相同数据结构的另一个数据集上工作过,但我不知道为什么我现在会收到此错误。我非常沮丧,因为我已经在互联网上搜索答案但一无所获。任何能够提供帮助的人都是天赐之物!

谢谢!

我们可以为此使用 if/else 条件 - 检查 'zotu.count' 中有多个唯一值的位置并应用 shapiro_test

library(rstatix)
library(dplyr)
library(tidyr)
dataset %>% 
  group_by(data.type, hour) %>%
  summarise(out = if(n_distinct(zotu.count) == 1) list(NA) 
    else list(shapiro_test(zotu.count)), .groups = 'drop') %>% 
  unnest(out)

-输出

# A tibble: 14 × 5
   data.type  hour variable   statistic p.value
   <chr>     <dbl> <chr>          <dbl>   <dbl>
 1 DNA           1 zotu.count     0.987   0.780
 2 DNA           2 zotu.count     0.832   0.194
 3 DNA          24 zotu.count     0.832   0.194
 4 DNA          48 zotu.count     0.812   0.144
 5 DNA          96 zotu.count     0.75    0    
 6 DNA         168 zotu.count     1       1.00 
 7 DNA         672 zotu.count     1       1.00 
 8 RNA           1 zotu.count     0.75    0    
 9 RNA           2 zotu.count     0.893   0.363
10 RNA          24 <NA>          NA      NA    
11 RNA          48 zotu.count     0.75    0    
12 RNA          96 zotu.count     0.75    0    
13 RNA         168 <NA>          NA      NA    
14 RNA         672 zotu.count     0.75    0    

我们也可以filter排除那些只有一个唯一值的组

dataset %>% 
   group_by(data.type, hour) %>% 
   filter(n_distinct(zotu.count) > 1) %>% 
   shapiro_test(zotu.count)
# A tibble: 12 × 5
   data.type  hour variable   statistic     p
   <chr>     <dbl> <chr>          <dbl> <dbl>
 1 DNA           1 zotu.count     0.987 0.780
 2 DNA           2 zotu.count     0.832 0.194
 3 DNA          24 zotu.count     0.832 0.194
 4 DNA          48 zotu.count     0.812 0.144
 5 DNA          96 zotu.count     0.75  0    
 6 DNA         168 zotu.count     1     1.00 
 7 DNA         672 zotu.count     1     1.00 
 8 RNA           1 zotu.count     0.75  0    
 9 RNA           2 zotu.count     0.893 0.363
10 RNA          48 zotu.count     0.75  0    
11 RNA          96 zotu.count     0.75  0    
12 RNA         672 zotu.count     0.75  0