dplyr n_distinct with condition

前端 未结 4 1511
离开以前
离开以前 2021-02-05 17:39

Using dplyr to summarise a dataset, I want to call n_distinct to count the number of unique occurrences in a column. However, I also want to do another summarise() for all uniqu

4条回答
  •  有刺的猬
    2021-02-05 18:07

    An alternative is to use the uniqueN function from data.table inside dplyr:

    library(dplyr)
    library(data.table)
    a %>% summarise(count_all = n_distinct(A), count_BisY = uniqueN(A[B == 'Y']))
    

    which gives:

      count_all count_BisY
    1         3          2
    

    You can also do everything with data.table:

    library(data.table)
    setDT(a)[, .(count_all = uniqueN(A), count_BisY = uniqueN(A[B == 'Y']))]
    

    which gives the same result.

提交回复
热议问题