【问题标题】:dplyr summarise with logical condition functionality [duplicate]具有逻辑条件功能的 dplyr 汇总 [重复]
【发布时间】:2018-07-20 09:48:53
【问题描述】:

我有一个逻辑数据表单,我想总结一下。

>test

# A tibble: 17 x 1
test 
<lgl>
1 NA   
2 FALSE
3 FALSE
4 FALSE
5 FALSE
6 FALSE
7 FALSE
8 FALSE
9 TRUE 
10 FALSE
11 FALSE
12 FALSE
13 FALSE
14 FALSE
15 FALSE

将其导入汇总函数可用于检查 NAs

> test %>% summarise(sum(is.na(test)))
# A tibble: 1 x 1
  `sum(is.na(test))`
               <int>
1                  1

但是我无法让它用于测试 FALSE 或 TRUE

> test %>% summarise(sum(test==TRUE))
# A tibble: 1 x 1
  `sum(test == TRUE)`
                <int>
1                  NA

> test %>% summarise(sum(test==FALSE))
# A tibble: 1 x 1
  `sum(test == FALSE)`
                 <int>
1                   NA

【问题讨论】:

  • 好吧,你是summing 与NA 的逻辑值,你需要删除它们。 test %&gt;% summarise(sum(test, na.rm = TRUE)) 也应该可以工作。

标签: r dplyr


【解决方案1】:

这是因为 NA 是求和的一部分。

Hadley Wickham 在这里提出了修复建议https://github.com/tidyverse/dplyr/issues/539

test %>% filter(!is.na(test)) %>% summarise(sum(test==FALSE))

# A tibble: 1 x 1
  `sum(test == FALSE)`
                 <int>
1                   15

我希望这可以节省其他人一些时间!

【讨论】:

    猜你喜欢
    • 2019-12-20
    • 2018-10-06
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2019-03-06
    • 2016-02-07
    相关资源
    最近更新 更多