# 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

Live Demo

Consider the below data frame −

set.seed(2)
ID<-1:25
Salary<-sample(20:50,25,replace=TRUE)
df<-data.frame(ID,Salary)
df

## Output

ID Salary
1 1 40
2 2 34
3 3 25
4 4 25
5 5 27
6 6 36
7 7 48
8 8 36
9 9 31
10 10 48
11 11 28
12 12 37
13 13 30
14 14 20
15 15 22
16 16 41
17 17 35
18 18 37
19 19 38
20 20 42
21 21 50
22 22 27
23 23 26
24 24 20
25 25 41

Splitting df based on Salary and creating new column for Salary Group having five groups −

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

Splitting df based on Salary and creating new column for Salary Group having three groups −

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: 21-Aug-2020

588 Views