我在任何地方都找不到如何在 Dplyr 中执行此操作的示例,但它肯定可以完成吗?
示例数据框:
x y z
this it 1
that them 2
that us 3
that you 4
Run Code Online (Sandbox Code Playgroud)
目标是这样的:
data %>%
filter(x %in% 'that') %>%
summarise(n_distinct(all dataframe cols))
Run Code Online (Sandbox Code Playgroud)
要返回这个:
x y z
1 3 3
Run Code Online (Sandbox Code Playgroud)
如果您仅引用一列,它会起作用,但是如何返回所有列的不同值的数量,而不n_distinct单独调用每一列?
summarise您可以使用and的作用域变体mutate,附加_all, _at, _if。
data.frame(x = c("this", "that", "that", "that"),
y = c("it", "them","us","you"),
z = c(1,2,3,4),
stringsAsFactors = F) %>%
filter(x %in% "that") %>%
summarise_all(n_distinct)
Run Code Online (Sandbox Code Playgroud)