Home > Software engineering >  Count occurrences of elements for a set of intervals in R
Count occurrences of elements for a set of intervals in R

Time:10-08

Lets say I have data like this

Number ID
1.5 X
2.4 X
3.1 Y
3.2 Y

My desired output is

ID 1 < x < 2 2 < x < 3 3 < x < 4
X 1 1 0
Y 0 0 2

What would be an efficient approach to creating this?

CodePudding user response:

We can create a column with cut and then use pivot_wider to reshape from 'long' to 'wide' format

library(dplyr)
library(tidyr)
df1 %>%
    mutate(grp = cut(Number, breaks = c(1, 2, 3, 4), 
      labels = c("1<x<2", "2<x<3", "3<x<4"))) %>% 
    pivot_wider(names_from = grp, values_from= Number,
         values_fn = length, values_fill = 0)

-output

# A tibble: 2 × 4
  ID    `1<x<2` `2<x<3` `3<x<4`
  <chr>   <int>   <int>   <int>
1 X           1       1       0
2 Y           0       0       2

data

df1 <- structure(list(Number = c(1.5, 2.4, 3.1, 3.2), ID = c("X", "X", 
"Y", "Y")), class = "data.frame", row.names = c(NA, -4L))
  •  Tags:  
  • r
  • Related