How to find the quartile for each value in an R vector?

R ProgrammingServer Side ProgrammingProgramming

Any numerical data can be divided into four parts (four quarters) 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 find the quartile (1 to 4) for each value in an R data frame column then we can use the quantile function and cut function as shown in the Examples given below.

Example 1

Following snippet creates a sample data frame −

x<-sample(1:50,200,replace=TRUE)
x

The following dataframe is created

[1] 13 6 19 50 5 26 2 33 47 27 32 27 40 21 13 36 10 24 31 15 37 30 26 8 8
[26] 27 9 26 22 6 38 41 4 45 44 45 44 1 2 13 8 42 9 33 27 4 47 19 41 17
[51] 30 41 18 33 42 44 11 30 29 2 6 34 28 42 3 46 41 17 26 7 3 35 20 41 43
[76] 30 35 4 33 19 13 20 40 17 16 39 21 29 3 48 16 36 17 38 14 5 17 31 15 15
[101] 15 2 14 22 30 23 34 26 19 30 12 22 50 25 23 5 11 42 24 13 24 3 39 41 47
[126] 29 44 34 37 5 5 25 28 15 47 5 8 38 20 35 46 41 36 23 44 17 12 37 20 45
[151] 39 22 45 37 22 20 33 49 30 25 34 36 37 26 31 6 25 47 26 26 3 42 49 44 21
[176] 21 30 8 45 39 28 23 50 50 6 39 5 11 4 39 13 37 3 31 36 14 35 3 46 48

Now, on the above created data frame, add the following code to the above snippet −

x<-sample(1:50,200,replace=TRUE)
Quartile_x<-cut(x,quantile(x),include.lowest=TRUE,labels=FALSE)
Quartile_x

Output

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

[1] 1 1 2 4 1 2 1 3 4 3 3 3 4 2 1 3 1 2 3 2 3 3 2 1 1 3 1 2 2 1 3 4 1 4 4 4 4
[38] 1 1 1 1 4 1 3 3 1 4 2 4 2 3 4 2 3 4 4 1 3 3 1 1 3 3 4 1 4 4 2 2 1 1 3 2 4
[75] 4 3 3 1 3 2 1 2 4 2 2 4 2 3 1 4 2 3 2 3 1 1 2 3 2 2 2 1 1 2 3 2 3 2 2 3 1
[112] 2 4 2 2 1 1 4 2 1 2 1 4 4 4 3 4 3 3 1 1 2 3 2 4 1 1 3 2 3 4 4 3 2 4 2 1 3
[149] 2 4 4 2 4 3 2 2 3 4 3 2 3 3 3 2 3 1 2 4 2 2 1 4 4 4 2 2 3 1 4 4 3 2 4 4 1
[186] 4 1 1 1 4 1 3 1 3 3 1 3 1 4 4

Example 2

Following snippet creates a sample data frame −

x1<-rnorm(60)
x1

The following dataframe is created

[1] -0.78320397 -1.89798980 -0.40849983 -0.48864748 0.65046057 0.75565752
[7] 0.90853116 0.12812570 -1.04673145 1.40538726 -0.36609681 -0.29007395
[13] -2.31607318 -0.50171092 0.56749487 1.82698086 -0.65585420 1.80345681
[19] -0.43615320 -0.19468978 1.39831067 0.02904616 -0.53014244 -1.46557667
[25] 0.73808190 0.13277697 -0.18977799 -1.05949807 -0.03586690 -0.82697923
[31] -2.12290352 0.29579464 -0.33013543 0.64853251 -0.63688176 0.40240103
[37] -0.97879263 -1.91852780 2.41123641 0.38460191 -0.20917679 0.08430901
[43] 0.08204956 0.94998564 -0.47617122 0.52763126 -0.38171950 -0.60330515
[49] 0.03002169 1.53267518 1.61017086 -0.75045333 1.33304594 -0.20378091
[55] 0.86007986 1.93331979 -0.15224071 -0.47710520 1.15332688 1.34159592

Now, on the above created data frame, add the following code to the above snippet −

x1<-rnorm(60)
Quartile_x1<-cut(x1,quantile(x1),include.lowest=TRUE,labels=FALSE)
Quartile_x1

Output

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

[1] 1 1 2 2 3 4 4 3 1 4 2 2 1 2 3 4 1 4 2 2 4 3 1 1 3 3 2 1 3 1 1 3 2 3 1 3 1 1
[39] 4 3 2 3 3 4 2 3 2 1 3 4 4 1 4 2 4 4 2 2 4 4

Example 3

Following snippet creates a sample data frame −

x2<-rpois(200,10)
x2

The following dataframe is created

[1] 10 7 10 8 13 6 3 9 11 12 8 5 7 7 8 15 14 14 10 6 15 8 10 7 11
[26] 18 7 18 14 12 8 10 9 11 6 7 13 11 9 13 9 14 5 12 4 10 9 7 10 8
[51] 9 4 6 7 11 9 7 10 5 10 10 10 8 9 6 8 10 18 10 8 5 12 14 13 11
[76] 7 12 15 10 17 15 8 7 4 7 14 14 8 6 11 10 12 6 12 11 7 4 2 6 10
[101] 14 5 9 10 12 13 9 9 13 7 5 13 4 8 9 3 12 11 7 5 7 5 15 8 15
[126] 12 10 7 12 7 7 8 8 7 7 6 9 9 10 7 12 6 13 10 8 12 7 14 11 15
[151] 12 7 10 9 13 15 6 12 9 12 10 4 10 9 12 11 13 11 9 11 7 9 14 16 12
[176] 3 8 9 16 8 5 9 9 11 9 14 14 10 8 12 10 8 10 13 9 8 5 6 11 11

Now, on the above created data frame, add the following code to the above snippet −

x2<-rpois(200,10)
Quartile_x2<-cut(x2,quantile(x2),include.lowest=TRUE,labels=FALSE)
Quartile_x2

Output

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

[1] 3 1 3 2 4 1 1 2 3 3 2 1 1 1 2 4 4 4 3 1 4 2 3 1 3 4 1 4 4 3 2 3 2 3 1 1 4
[38] 3 2 4 2 4 1 3 1 3 2 1 3 2 2 1 1 1 3 2 1 3 1 3 3 3 2 2 1 2 3 4 3 2 1 3 4 4
[75] 3 1 3 4 3 4 4 2 1 1 1 4 4 2 1 3 3 3 1 3 3 1 1 1 1 3 4 1 2 3 3 4 2 2 4 1 1
[112] 4 1 2 2 1 3 3 1 1 1 1 4 2 4 3 3 1 3 1 1 2 2 1 1 1 2 2 3 1 3 1 4 3 2 3 1 4
[149] 3 4 3 1 3 2 4 4 1 3 2 3 3 1 3 2 3 3 4 3 2 3 1 2 4 4 3 1 2 2 4 2 1 2 2 3 2
[186] 4 4 3 2 3 3 2 3 4 2 2 1 1 3 3

Example 4

Following snippet creates a sample data frame −

x3<-sample(100:999,200)
x3

The following dataframe is created

[1] 752 118 749 608 365 857 420 294 369 596 594 489 871 712 242 195 934 406
[19] 127 578 826 591 605 918 508 239 542 688 581 963 609 696 390 571 209 310
[37] 449 173 659 236 502 874 326 375 809 158 143 372 448 621 858 259 282 679
[55] 428 429 730 363 250 743 851 634 507 593 468 475 984 401 513 916 599 112
[73] 764 654 298 897 156 432 994 890 991 551 261 240 206 267 486 872 260 989
[91] 765 937 313 622 729 560 784 413 631 460 197 305 971 555 529 960 307 708
[109] 682 171 385 912 281 146 812 931 814 347 215 303 705 237 780 868 465 834
[127] 229 586 838 135 368 919 843 378 110 196 837 604 895 577 554 620 902 338
[145] 618 860 253 422 331 862 522 859 339 278 922 998 412 166 220 455 122 828
[163] 640 894 783 653 842 346 500 204 380 544 354 569 570 447 714 572 463 969
[181] 773 300 719 100 643 668 791 600 451 407 534 518 666 839 211 996 425 476
[199] 831 724

Now, on the above created data frame, add the following code to the above snippet −

x3<-sample(100:999,200)
Quartile_x3<-cut(x3,quantile(x3),include.lowest=TRUE,labels=FALSE)
Quartile_x3

Output

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

[1] 3 1 3 3 2 4 2 1 2 3 3 2 4 3 1 1 4 2 1 3 4 3 3 4 2 1 2 3 3 4 3 3 2 3 1 1 2
[38] 1 3 1 2 4 1 2 4 1 1 2 2 3 4 1 1 3 2 2 3 2 1 3 4 3 2 3 2 2 4 2 2 4 3 1 3 3
[75] 1 4 1 2 4 4 4 2 1 1 1 1 2 4 1 4 3 4 1 3 3 3 4 2 3 2 1 1 4 3 2 4 1 3 3 1 2
[112] 4 1 1 4 4 4 2 1 1 3 1 4 4 2 4 1 3 4 1 2 4 4 2 1 1 4 3 4 3 2 3 4 1 3 4 1 2
[149] 1 4 2 4 1 1 4 4 2 1 1 2 1 4 3 4 4 3 4 2 2 1 2 2 2 3 3 2 3 3 2 4 4 1 3 1 3
[186] 3 4 3 2 2 2 2 3 4 1 4 2 2 4 3

Example 5

Following snippet creates a sample data frame −

x4<-rexp(80)
x4

The following dataframe is created

[1] 1.704391936 0.846305793 2.634730579 0.046713812 0.071035354 1.601917619
[7] 0.877783528 0.347054014 0.255256684 1.412506510 2.077939028 0.374561070
[13] 0.316267411 1.594126495 3.336048141 0.838570721 1.363036623 0.203032363
[19] 0.649302468 0.650347426 0.740160604 0.156123491 0.055717936 0.042832276
[25] 2.197125647 0.324367832 1.280220649 0.404356963 0.233069157 0.546131155
[31] 0.755824809 3.504490396 0.186715212 0.232005058 7.800082410 0.148598746
[37] 2.139379242 1.467609948 0.297341814 0.972752983 0.127346731 2.013999749
[43] 0.811660543 0.596504302 0.706068856 0.519330764 0.056232081 2.482256435
[49] 5.506130742 0.500306063 4.595401484 0.452151737 1.673576768 1.529217449
[55] 1.188213197 2.057031842 0.723472035 0.877388051 0.749470539 1.944659069
[61] 1.408241730 1.603073174 2.624687398 0.762815667 0.066609307 0.818458569
[67] 0.043528649 0.977237351 1.968671540 1.100291921 0.460354839 0.451059832
[73] 0.005754929 0.227403548 0.700687943 1.563982766 0.285281364 0.115223158
[79] 0.480592353 0.183460258

Now, on the above created data frame, add the following code to the above snippet −

x4<-rexp(80)
Quartile_x4<-cut(x4,quantile(x4),include.lowest=TRUE,labels=FALSE)
Quartile_x4

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

[1] 4 3 4 1 1 4 3 2 1 3 4 2 2 4 4 3 3 1 2 2 2 1 1 1 4 2 3 2 1 2 3 4 1 1 4 1 4 3
[39] 2 3 1 4 3 2 2 2 1 4 4 2 4 2 4 3 3 4 2 3 3 4 3 4 4 3 1 3 1 3 4 3 2 2 1 1 2 3
[77] 1 1 2 1
raja
Updated on 01-Nov-2021 06:05:00

Advertisements