[英]r cut report results for all intervals, including empty ones
I have some data that I'd like to count occurrences in breaks, such as the following.我有一些数据,我想计算中断的出现次数,如下所示。 The runif statement results in a vector with no zeros, so I create two data frames, one with and one without an added zero
runif 语句生成一个不带零的向量,因此我创建了两个数据帧,一个带有零,一个不带零
library(dplyr)
breaks <- c(0, 1, 25, 50, 75, 100)
testValues <- runif(50, min = 0, max = 100)
testValues_df <- data.frame(lyr1 = testValues)
testValues_w0 <- c(testValues, 0)
testValues_w0_df <- data.frame(lyr1 = testValues_w0)
testValues_df %>%
group_by(gr=cut(lyr1, breaks= breaks, include.lowest = FALSE, right = FALSE) ) %>%
summarise(n= n()) %>%
arrange(as.numeric(gr))
testValues_w0_df %>%
group_by(gr=cut(lyr1, breaks= breaks, include.lowest = FALSE, right = FALSE) ) %>%
summarise(n= n()) %>%
arrange(as.numeric(gr))
The result is结果是
# A tibble: 5 × 2
gr n
<fct> <int>
1 [0,1) 1
2 [1,25) 12
3 [25,50) 11
4 [50,75) 18
5 [75,100) 9
However, if I don't add the 0 to the data vector I get this.但是,如果我不将 0 添加到数据向量中,我会得到这个。
A tibble: 4 × 2
gr n
<fct> <int>
1 [1,25) 12
2 [25,50) 11
3 [50,75) 18
4 [75,100) 9
Is there some way to force the second output to include [0,1] 0?有没有办法强制第二个 output 包含 [0,1] 0?
We can use complete
afterwards之后我们可以使用
complete
的
library(dplyr)
library(tidyr)
testValues_w0_df %>%
group_by(gr=cut(lyr1, breaks= breaks, include.lowest = FALSE,
right = FALSE) ) %>%
summarise(n= n(), .groups = 'drop') %>%
arrange(as.numeric(gr)) %>%
complete(gr = levels(gr), fill = list(n = 0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.