Suppose that I have dataset, created by this code:
set.seed(3192)
Data <- data.frame(
X = sample(50,20),
Y = sample(letters[1:4], 20, replace = TRUE)
)
Data <- Data[do.call(order,Data),]
Data
## X Y
## 2 b
## 3 c
## 4 c
## ...
## ...
## 47 c
## 49 d
## 50 a
I want to count occurrence of "C" in the column Y, by scanning window size of 10 in column X. So, the output will be look like this:
X_Range Count_of_c
1-10 2
11-20 0
21-30 4
31-40 1
41-50 1
Is there anyone have an idea?
You can use cut
function to create group onyour x column and then group by this cut
column to calculate the count (here, I'm using dplyr
):
library(dplyr)
Data %>% mutate(Cut = cut(X, breaks = seq(0,50, by = 10))) %>%
group_by(Cut,Y,.drop = FALSE) %>%
summarise(Count_of_c = n()) %>%
filter(Y =="c")
# A tibble: 5 x 3
# Groups: Cut [5]
Cut Y Count_of_c
<fct> <fct> <int>
1 (0,10] c 0
2 (10,20] c 0
3 (20,30] c 1
4 (30,40] c 1
5 (40,50] c 3
Does it look what you are expecting?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.