是否可以在 R 中添加 summarize(count = n_distinct(x)) 异常?
Is it possible to add an exception to summarize(count = n_distinct(x)) in R?
是否可以在 R 中向 summarize(count = n_distinct(x))
添加异常,同时允许 "normal" summarize(count = n()) function
计算异常?
如何组合计数 n()
和 n_distinct()
函数来创建一个新列?
这样,我可以在 x
列中总结观察的不同计数,同时以观察的形式添加异常,这将不限于不同的计数,而是受"normal" summarize(count = n()) function
.
例如,如果 x = c(1, 2, 2, 4, 5, 8, 8, ..., 99)
,我可以总结所有观察值的不同计数,例如 x
列中的观察值 8
。观察结果 8
将受制于 summarize(count = n()) function
。然后这将计算 8
的数量加上 x
.
中其他唯一值的数量
总而言之,这将创建一个新列 "count",其中所有值都将来自不同的计数,除了一个例外,其值将来自 "normal" 计数.
给未来读者的更新:
如果您想结合不同的计数和 "normal" 计数函数,这将清楚地计算 x
中的所有观察值,观察值 8
除外,它将是到 "normal" 计数:
summarize(count = n_distinct(x[x != 8]) + sum(x == 8))
这将计算 8
的数量加上 x
中其他唯一值的数量。
但是,如果您想要使用非重复计数函数,同时添加一个根本不应该被计数的异常(例如 8
),请这样写:
n_distinct(x[x != 8])
或者这个
... %>% filter(x != 8) %>% summarize...
是否可以在 R 中向 summarize(count = n_distinct(x))
添加异常,同时允许 "normal" summarize(count = n()) function
计算异常?
如何组合计数 n()
和 n_distinct()
函数来创建一个新列?
这样,我可以在 x
列中总结观察的不同计数,同时以观察的形式添加异常,这将不限于不同的计数,而是受"normal" summarize(count = n()) function
.
例如,如果 x = c(1, 2, 2, 4, 5, 8, 8, ..., 99)
,我可以总结所有观察值的不同计数,例如 x
列中的观察值 8
。观察结果 8
将受制于 summarize(count = n()) function
。然后这将计算 8
的数量加上 x
.
总而言之,这将创建一个新列 "count",其中所有值都将来自不同的计数,除了一个例外,其值将来自 "normal" 计数.
给未来读者的更新:
如果您想结合不同的计数和 "normal" 计数函数,这将清楚地计算 x
中的所有观察值,观察值 8
除外,它将是到 "normal" 计数:
summarize(count = n_distinct(x[x != 8]) + sum(x == 8))
这将计算 8
的数量加上 x
中其他唯一值的数量。
但是,如果您想要使用非重复计数函数,同时添加一个根本不应该被计数的异常(例如 8
),请这样写:
n_distinct(x[x != 8])
或者这个
... %>% filter(x != 8) %>% summarize...