How to find n number of quartiles for every row in R?


To find the n number of quartiles for every row in an R data frame, we can use apply function along with quantile function.

For example, if we have a data frame called df that contains hundred rows and we want to find two quartiles say first and third for each row then we can use the below mentioned command −

apply(df,1,quantile,c(0.25,0.75))

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,1)
x2<-rpois(20,2)
x3<-rpois(20,5)
x4<-rpois(20,10)
df1<-data.frame(x1,x2,x3,x4)
df1

The following dataframe is created −

   x1 x2 x3 x4
1  0  2  5  12
2  0  2  5  11
3  2  1  6   9
4  3  1  6   8
5  1  1  9  11
6  0  4  3   8
7  1  2  5   7
8  1  0  5  10
9  1  4  8  11
10 4  2  5  14
11 0  5  7   9
12 1  2  9  10
13 1  2  8  11
14 0  1  2   9
15 1  0  3  10
16 1  3  6  11
17 0  1  3  11
18 2  1  1   7
19 1  1  7   8
20 0  3  5   9

To find the first quartile, second quartile, and third quartile for each row in df1, add the following code to the above snippet −

x1<-rpois(20,1)
x2<-rpois(20,2)
x3<-rpois(20,5)
x4<-rpois(20,10)
df1<-data.frame(x1,x2,x3,x4)
apply(df1,1,quantile,c(0.25,0.50,0.75))

Output

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

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
25% 1.50 1.5  1.75 2.5  1.0  2.25 1.75 0.75 3.25 3.50  3.75  1.75  1.75  0.75
50% 3.50 3.5  4.00 4.5  5.0  3.50 3.50 3.00 6.00 4.50  6.00  5.50  5.00  1.50
75% 6.75 6.5 6.75 6.5 9.5 5.00 5.50 6.25 8.75 7.25 7.50 9.25 8.75 3.75
  [,15] [,16] [,17] [,18] [,19] [,20]
25% 0.75 2.50 0.75  1.00  1.00  2.25
50% 2.00 4.50 2.00  1.50  4.00  4.00
75% 4.75 7.25 5.00  3.25  7.25  6.00

Example 2

Following snippet creates a sample data frame −

y1<-round(rnorm(20),2)
y2<-round(rnorm(20),2)
y3<-round(rnorm(20),2)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created −

     y1     y2     y3
1  -1.41   1.96  -0.23
2  -0.27  -0.05  -0.51
3   0.72   0.40  -0.93
4   1.07   1.68  -0.82
5   0.34  -0.86  -0.29
6   1.67  -0.13   1.60
7   0.38   0.57  -0.28
8  -0.91   0.39   0.61
9   0.99   0.75   0.21
10  0.40   0.74  -2.61
11  1.52   0.97   0.92
12  1.35   0.14   0.04
13 -0.51   0.49  -0.29
14  0.46   0.07  -1.62
15 -0.51  -1.55   0.81
16 -0.65  -0.20   0.96
17  0.68  -0.75   0.65
18 -0.34   0.22  -0.26
19 -0.59  -0.28  -0.09
20  1.98   0.85   0.15

To find the first quartile, second quartile, and third quartile for each row in df1, add the following code to the above snippet −

apply(df1,1,quantile,c(0.25,0.75))

Output

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

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
25% 1.50 1.5  1.75 2.5  1.0  2.25 1.75 0.75 3.25 3.50  3.75  1.75  1.75  0.75
75% 6.75 6.5  6.75 6.5  9.5  5.00 5.50 6.25 8.75 7.25  7.50  9.25  8.75  3.75
     [,15] [,16] [,17] [,18] [,19] [,20]
25% 0.75  2.50   0.75  1.00  1.00  2.25
75% 4.75  7.25   5.00  3.25  7.25  6.00

Updated on: 12-Nov-2021

593 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements