How to a split a continuous variable into multiple groups in R?

Splitting a continuous variable is required when we want to compare different levels of a categorical variable based on some characteristics of the continuous variable. For example, creating the salary groups from salary and then comparing those groups using analysis of variance or Kruskal-Wallis test. To split a continuous variable into multiple groups we can use cut2 function of Hmisc package −

Example

df$Salary_Group<-as.numeric(cut2(df$Salary, g=3))
df
ID Salary Salary_Group
1 1 40 3
2 2 34 2
3 3 25 1
4 4 25 1
5 5 27 1
6 6 36 2
7 7 48 3
8 8 36 2
9 9 31 2
10 10 48 3
11 11 28 1
12 12 37 2
13 13 30 2
14 14 20 1
15 15 22 1
16 16 41 3
17 17 35 2
18 18 37 2
19 19 38 3
20 20 42 3
21 21 50 3
22 22 27 1
23 23 26 1
24 24 20 1
25 25 41 3
df$Salary_Group
[1] 3 2 1 1 1 2 3 2 2 3 1 2 2 1 1 3 2 2 3 3 3 1 1 1 3

Here, the group sizes are different because the sample size is 25 which is not a multiple of 3.

Updated on: 2026-03-11T23:22:53+05:30

882 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements