R

How to categorize numbers in R

How to categorize numbers in R

Here is my approach on how to categorize numbers by user-defined groups with R. When you have a few and predictable groups, you can use the ifelse function. But sometimes, there is a long list of numbers that are changing over time.

Categorize numbers with plyr and dplyr

To categorize numbers, I will use R built-in dataset ChickWeight and R package plyr. There is a column called weight that I would like to categorize in groups by hundreds (1 to 100, 101 to 200, etc.).

cw <- ChickWeight

require(plyr)

cw$weight_from <- round_any(cw$weight - 1, 100, f = floor) + 1
cw$weight_to <- round_any(cw$weight, 100, f = ceiling)

If you have to get a count of each category values, then you can do that with dplyr like this.

require(dplyr)

cw %>%
group_by(weight_from, weight_to) %>%
summarise(cnt = n()) %>%
knitr::kable()

data categorization with plyr

Categorize numbers with dplyr

Of course, there are other ways to group numbers in R. For example, you can do that with dplyr and cut function.

require(dplyr)

cw <- ChickWeight

cw %>%
group_by(g = cut(weight, breaks= seq(0, 400, by = 100)) ) %>%
summarise(cnt= n()) %>%
knitr::kable()

data categorization with dplyr

Here is another approach with a cut function if you need to label your numbers.

Check out my favorite RStudio tips and tricks.




0 comments on “How to categorize numbers in R

Leave a Reply

Your email address will not be published. Required fields are marked *