在dplyr的group_by中使用过滤器的问题

时间:2018-12-05 11:45:15

标签: r group-by dplyr summary

我环顾四周,尝试了不同的解决方案,但似乎没有发现任何问题。我相信它很容易解决.....

我在数据框Class中有9个分类类(a)上的98个数据点。每个数据点都有自己的p.value(p.value),它们在列中有所不同。

我需要使用Class中的group_by函数在dplyr上完成各种操作(请参见下文)。

对于我的AICorrect列,我需要计算p.value < 1的按类别分组的总p。值。

我具有以下功能

output <- as.data.frame(a %>%
                          group_by(Class) %>%
                          summarise(Abundance = length(Class),
                                    Density = length(Class) / h2o_m3,
                                    AICorrect = length(count(p.value < 1)) ## issue here
                                    ))

“没有”并报告以下错误:

Error in summarise_impl(.data, dots) : 
  Evaluation error: no applicable method for 'groups' applied to an object of class "logical".

任何见识都会很棒!

谢谢

(警告下方的令人讨厌的代码)

H20

h20_m3 <- 0.3424667

dput输出的前20行

structure(list(Lat = structure(c(3L, 3L, 2L, 2L, 1L, 4L, 3L, 
3L, 2L, 2L, 5L, 6L, 3L, 3L, 2L, 2L, 7L, 8L, 3L, 3L), .Label = c("51  12.93257'", 
"51  12.93378'", "51  13.0399'", "51  13.20549'", "52  12.93257'", 
"52  13.20549'", "53  12.93257'", "53  13.20549'", "54  12.93257'", 
"54  13.20549'", "55  12.93257'", "55  13.20549'", "56  12.93257'", 
"56  13.20549'", "57  12.93257'", "57  13.20549'", "58  12.93257'", 
"58  13.20549'", "59  12.93257'", "59  13.20549'", "60  12.93257'", 
"60  13.20549'", "61  12.93257'", "61  13.20549'", "62  12.93257'", 
"62  13.20549'", "63  12.93257'", "63  13.20549'", "64  12.93257'", 
"64  13.20549'", "65  12.93257'", "65  13.20549'", "66  12.93257'", 
"66  13.20549'"), class = "factor"), Long = structure(c(3L, 3L, 
2L, 2L, 1L, 4L, 3L, 3L, 2L, 2L, 1L, 4L, 3L, 3L, 2L, 2L, 1L, 4L, 
3L, 3L), .Label = c("- 5   1.23944'", "- 5   1.23960'", "- 5   1.33092'", 
"- 5   1.36293'"), class = "factor"), Date = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = "22/11/2018", class = "factor"), Time = structure(c(1L, 
2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 
6L, 7L, 8L), .Label = c("03:22:40", "03:22:41", "03:22:42", "03:22:43", 
"03:22:44", "03:22:46", "03:22:47", "03:22:49", "03:22:57", "03:22:59", 
"03:23:01", "03:23:05", "03:23:06", "03:23:11", "03:23:14", "03:23:16", 
"03:23:17", "03:23:20", "03:23:21", "03:23:24", "03:23:26", "03:23:29", 
"03:23:30", "03:23:31", "03:23:32", "03:23:33", "03:23:34", "03:23:38", 
"03:23:41", "03:23:44", "03:23:46", "03:23:53", "03:23:57", "03:24:00", 
"03:24:05", "03:24:08", "03:24:13", "03:24:14", "03:24:17", "03:24:20", 
"03:24:22", "03:24:28", "03:24:30", "03:24:32", "03:24:33", "03:24:36", 
"03:24:37", "03:24:44", "03:24:47", "03:24:56", "03:25:09", "03:25:11", 
"03:25:12", "03:25:17", "03:25:21", "03:25:36", "03:25:46", "03:25:50", 
"03:25:56", "03:26:02", "03:26:07", "03:26:09", "03:26:12", "03:26:23", 
"03:26:31", "03:26:40", "03:27:03", "03:27:41", "03:27:50", "03:27:57", 
"03:28:21", "03:38:14"), class = "factor"), Class = structure(c(9L, 
9L, 1L, 9L, 9L, 9L, 8L, 1L, 9L, 9L, 7L, 2L, 9L, 1L, 8L, 1L, 9L, 
9L, 1L, 6L), .Label = c("AirBubbles", "Bryozoa_Larvae", "Chaetognath", 
"Copepod_Cyclopoid_Corycaeus_spp", "Copepod_Harpacticoid", "Detritus", 
"Doliolids", "Fish_eggs", "Krill_Nauplii", "Polychaete_larvae"
), class = "factor"), p.value = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 0.6, 1, 0.3, 1), Area = c(4618.5, 3456.5, 1664, 
1124.5, 808, 624.5, 1188, 4905, 4098.5, 1622, 965, 5112.5, 3600.5, 
1329.5, 419.5, 1946.5, 859.5, 1461, 2101, 1808), MajAx = c(160.331, 
122.041, 48.5977, 134.161, 79.0913, 82.2169, 52, 136.107, 107.077, 
66.3892, 77.7797, 144.534, 101.12, 50.1081, 35.8204, 59.397, 
49, 71.2319, 81.2104, 69), MinAx = c(87.2272, 48.9879, 44.701, 
34.8173, 39.0966, 13.6236, 44, 78.8193, 74.0917, 52.1584, 54.5753, 
81.2652, 72.304, 38.059, 19.7893, 50.2046, 22, 43.9899, 42.3231, 
56)), row.names = c(NA, 20L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

问题在这里:

count(a$p.value < 1)

出现此错误:

  

UseMethod(“ groups”)中的错误:'groups'没有适用的方法   应用于“逻辑”类的对象

发生这种情况是因为count调用了group_byungroup

您可以将count替换为sum,以计算所有TRUE值。

output <- as.data.frame(a %>%
                          group_by(Class) %>%
                          summarise(Abundance = length(Class),
                                    Density = length(Class) / h2o_m3,
                                    AICorrect = sum(p.value < 1) ## issue here
                          ))