dplyr n_distinct with condition

前端 未结 4 1550
离开以前
离开以前 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:29

    This produces the distinct A counts by each value of B using dplyr.

    library(dplyr)
    a %>%
      group_by(B) %>%
      summarise(count = n_distinct(A))
    

    This produces the result:

    Source: local data frame [2 x 2]
    
           B count
      (fctr) (int)
    1      N     1
    2      Y     2
    

    To produce the desired output added above using dplyr, you can do the following:

    a %>% summarise(count_all = n_distinct(A), count_BisY = length(unique(A[B == 'Y'])))
    

    This produces the result:

      count_all count_BisY
    1         3          2
    

提交回复
热议问题