How to create bin frequency table in R?


The bin frequency table is the table that represent the frequency for a range of values in a particular variable, in R we generally store these variables in a vector or a column of an R data frame. If we want to create a bin frequency table then table function with cut and breaks function can be used.

Example1

Live Demo

> x1<-round(runif(200,2,5))
> x1

Output

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

Example

> as.data.frame(table(cut(x1,breaks=seq(2,5,by=1))))

Output

Var1 Freq
1 (2,3] 67
2 (3,4] 67
3 (4,5] 29

Example2

Live Demo

> x2<-round(runif(200,1,20))
> x2

Output

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

Example

> as.data.frame(table(cut(x2,breaks=seq(1,20,by=1))))

Output

Var1 Freq
1 (1,2] 13
2 (2,3] 9
3 (3,4] 5
4 (4,5] 17
5 (5,6] 16
6 (6,7] 12
7 (7,8] 12
8 (8,9] 8
9 (9,10] 9
10 (10,11] 14
11 (11,12] 10
12 (12,13] 12
13 (13,14] 6
14 (14,15] 12
15 (15,16] 10
16 (16,17] 7
17 (17,18] 10
18 (18,19] 7
19 (19,20] 3

Example3

Live Demo

> x3<-sample(0:9,200,replace=TRUE)
> x3

Output

[1] 0 6 1 5 7 1 1 4 6 1 2 4 7 0 8 1 5 9 0 4 2 3 1 9 0 3 7 0 7 3 5 0 2 6 4 7 8
[38] 0 8 5 3 3 2 8 7 0 0 5 4 1 8 9 0 1 2 4 3 2 8 4 2 4 9 3 8 5 9 8 7 1 4 9 9 9
[75] 1 1 4 4 8 6 2 2 1 4 9 4 6 1 5 3 6 4 8 3 8 7 9 9 7 8 5 5 3 9 0 9 0 8 0 9 0
[112] 1 7 3 8 5 6 6 0 2 2 8 2 8 9 5 4 7 8 9 7 0 3 5 0 4 8 7 3 1 6 0 0 4 0 2 3 9
[149] 4 6 8 2 6 6 5 7 1 0 1 8 3 4 2 9 3 9 5 7 0 5 6 1 9 9 7 3 3 9 6 0 0 7 6 1 2
[186] 6 4 8 6 9 0 4 7 0 4 6 4 2 0 4

Example

> as.data.frame(table(cut(x3,breaks=seq(0,9,by=1))))

Output

Var1 Freq
1 (0,1] 19
2 (1,2] 17
3 (2,3] 18
4 (3,4] 24
5 (4,5] 15
6 (5,6] 18
7 (6,7] 18
8 (7,8] 21
9 (8,9] 23

Example4

Live Demo

> x4<-sample(1:10,200,replace=TRUE)
> x4

Output

[1] 8 5 5 10 1 4 5 3 8 4 7 9 2 2 9 2 9 5 3 9 10 6 6 2 8
[26] 10 6 7 8 8 1 8 6 5 2 10 3 5 1 1 1 6 3 9 4 6 1 9 5 10
[51] 3 7 3 2 2 6 6 1 9 9 6 10 2 3 10 7 7 1 8 4 6 4 4 3 4
[76] 6 6 2 3 9 5 9 5 10 2 9 2 8 5 6 10 9 7 10 2 3 2 10 9 10
[101] 6 2 10 5 10 8 9 3 6 8 2 5 1 2 8 5 2 2 3 5 9 1 8 8 6
[126] 9 7 1 4 2 4 9 5 4 7 4 1 7 10 5 5 8 9 2 9 10 1 7 1 5
[151] 7 4 9 9 9 3 6 10 6 2 3 9 5 1 6 8 10 8 8 3 2 2 7 8 9
[176] 10 3 3 1 1 8 9 6 5 3 2 6 4 4 7 10 6 2 10 4 4 1 1 1 7

Example

> as.data.frame(table(cut(x4,breaks=seq(0,10,by=2))))

Output

Var1 Freq
1 (0,2] 45
2 (2,4] 34
3 (4,6] 42
4 (6,8] 33
5 (8,10] 46

Example5

Live Demo

> x5<-sample(501:520,100,replace=TRUE)
> x5

Output

[1] 502 513 519 518 515 520 517 505 501 518 507 517 508 520 505 505 504 505
[19] 506 509 518 519 505 516 511 507 509 505 506 501 505 504 514 504 519 510
[37] 512 501 517 501 516 511 514 503 508 504 513 520 502 502 516 505 502 516
[55] 515 511 507 512 519 515 509 501 502 501 509 502 518 517 516 506 520 518
[73] 514 519 511 515 520 517 519 514 520 510 502 510 508 503 503 514 515 512
[91] 510 517 505 512 517 517 520 501 513 516

Example

> as.data.frame(table(cut(x5,breaks=seq(500,520,by=2))))

Output

Var1 Freq
1 (500,502] 14
2 (502,504] 7
3 (504,506] 12
4 (506,508] 6
5 (508,510] 8
6 (510,512] 8
7 (512,514] 8
8 (514,516] 11
9 (516,518] 13
10 (518,520] 13

Updated on: 07-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements