How to create a frequency table in data frame format in R?

R ProgrammingServer Side ProgrammingProgramming

To create a frequency table in R, we can simply use table function but the output of table function returns a horizontal table. If we want to read the table in data frame format then we would need to read the table as a data frame using as.data.frame function. For example, if we have a table called T then to convert it into a data frame format we can use the command as.data.frame(T).

Example1

Live Demo

> x1<-rpois(200,2)
> x1

Output

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

Example

> table_x1<-table(x1)
> table_x1

Output

x1
0 1 2 3 4 5 6
27 58 39 42 20 11 3

Example

> df1<-as.data.frame(table_x1)
> df1

Output

  x1 Freq
1 0   27
2 1   58
3 2   39
4 3   42
5 4   20
6 5   11
7 6   3

Example2

Live Demo

> x2<-rpois(200,10)
> x2

Output

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

Example

> table_x2<-table(x2)
> table_x2

Output

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

Example

> df2<-as.data.frame(table_x2)
> df2

Output

   x2 Freq
1  3   3
2  4   5
3  5   6
4  6   15
5  7   17
6  8   20
7  9   22
8  10  23
9  11  21
10 12  18
11 13  16
12 14  12
13 15  5
14 16  8
15 17  5
16 18  2
17 19  1
18 20  1

Example3

Live Demo

> x3<-rpois(200,8)
> x3

Output

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

Example

> table_x3<-table(x3)
> table_x3

Output

x3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 2 6 12 20 15 29 34 26 11 15 10 8 5 2 3 1

Example

> df3<-as.data.frame(table_x3)
> df3

Output

   x3 Freq
1  1   1
2  2   2
3  3   6
4  4   12
5  5   20
6  6   15
7  7   29
8  8   34
9  9   26
10 10  11
11 11  15
12 12  10
13 13  8
14 14  5
15 15  2
16 16  3
17 17  1

Example4

Live Demo

> x4<-rpois(200,5)
> x4

Output

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

Example

> table_x4<-table(x4)
> table_x4
x4

Output

1 2 3 4 5 6 7 8 9 10 12
10 17 29 25 40 35 15 17 6 5 1

Example

> df4<-as.data.frame(table_x4)
> df4

Output

   x4 Freq
1  1   10
2  2   17
3  3   29
4  4   25
5  5   40
6  6   35
7  7   15
8  8   17
9  9   6
10 10  5
11 12  1
raja
Published on 04-Mar-2021 08:13:05
Advertisements