How to find the first quartile for a data frame column in R?

R ProgrammingServer Side ProgrammingProgramming

The first quartile is the value that exists at the 25th percentile, that means there are 25% of values in the data that lie below first quartile. When we find the summary of data frame the output returns this value but if we want to extract only the first quartile then quantile function can be used by specifying the percentage using 0.25.

Consider the below data frame −

Example

 Live Demo

x<-rnorm(20)
df1<-data.frame(x)
df1

Output

      x
1  -2.0468677
2   1.6699889
3   0.6812778
4  -1.2456486
5   0.9066429
6   0.4191929
7   1.0230824
8  -1.5463724
9   1.0890182
10  -1.7451677
11   0.3218517
12  -0.2812037
13   0.2178108
14   0.4074959
15  -1.9794296
16   0.2920860
17   0.9381717
18  -0.3378574
19  -1.1992889
20  -0.4656026

Finding the first quartile of x −

quantile(df1$x,0.25)

25%
-1.210879

Example

 Live Demo

y<-rpois(20,5)
df2<-data.frame(y)
df2

Output

   y
1  7
2  6
3  3
4  4
5  2
6  8
7  6
8  5
9  3
10 1
11 6
12 1
13 3
14 5
15 9
16 3
17 6
18 3
19 4
20 5

Finding the first quartile of y −

quantile(df2$y,0.25)

25%
3

Example

 Live Demo

z<-rexp(20,3.2)
df3<-data.frame(z)
df3

Output

     z
1  0.447126145
2  0.349641120
3  1.013647257
4  1.074568795
5  0.351561016
6  0.253472100
7  0.083377626
8  0.432579828
9  0.244444691
10  0.003626295
11  0.396824568
12  0.392368738
13  0.233784862
14  0.103773163
15  0.176434586
16  0.177519043
17  0.016000915
18  0.290995177
19  0.031436406
20  0.389733464

Finding the first quartile of z −

quantile(df3$z,0.25)

25%
0.1582692
raja
Published on 06-Feb-2021 08:28:19
Advertisements