Create a quartile column for each value in an R data frame column.

Any numerical data can be divided into four parts by using three quartiles, first quartile at 25%, second quartile at 50% and third quartile at 75% hence there will be four quarters to represent first 25%, second 25%, third 25% and the last 25% in a set of data.

If we want to create a quartile (1 to 4) column for each value in an R data frame column then we can use the quantile function and cut function as shown in the below Examples.

Example 1

Following snippet creates a sample data frame −

x<-sample(1:50,20)
df1<-data.frame(x)
df1

The following dataframe is created

    x
1   4
2  44
3   1
4  14
5   5
6  18
7   3
8  16
9  41
10 10
11 35
12 48
13 36
14 23
15 17
16 19
17 11
18 43
19 28
20 29

To create a quartile column for column x in df1 on the above created data frame, add the following code to the above snippet −

x<-sample(1:50,20)
df1<-data.frame(x)
df2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

            y Quartile
1  -0.08949509    3
2  -0.12711363    3
3  -0.52805367    2
4   0.08087408    3
5  -1.35069115    1
6   0.13678392    3
7  -0.41386292    3
8  -0.80830050    2
9  -1.13387570    2
10 -1.56282579    1
11 -1.27191819    1
12  1.10834061    4
13 -1.53450425    1
14  0.83568645    4
15 -0.52896185    2
16  0.45211521    4
17 -1.45162982    1
18 -0.63935428    2
19  1.71258558    4
20  1.09091493    4

Updated on: 05-Nov-2021

2K+ Views