Syntax error How to access the table values in R?

How to access the table values in R?



Sometimes we want to extract table values, especially in cases when we have a big table. This helps us to understand the frequency for a particular item in the table. To access the table values, we can use single square brackets. For example, if we have a table called TABLE then the first element of the table can accessed by using TABLE[1].

Example1

 Live Demo

x1<−rpois(200,5)
x1

Output

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

Example

Table1<−table(x1)
Table1

Output

x1
0 1 2 3 4 5 6 7 8 9 10 11 14 16
1 4 14 31 35 39 29 19 12 7 3 4 1 1

Example

Table1[1]

Output

0
1

Example

Table1[2]

Output

1
4

Example

Table1[5]

Output

4
35

Example

Table1[10]

Output

9
7

Example2

 Live Demo

x2<−rpois(200,10)
x2

Output

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

Example

Table2<−table(x2)
Table2

Output

x2
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20
3 3 1 6 11 17 26 25 26 20 16 16 7 7 9 5 1 1

Example

Table2[2]

Output

3
3

Example

Table2[10]

Output

11
20

Example

Table2[15]

Output

16
9

Example3

 Live Demo

x3<−sample(501:510,200,replace=TRUE)
x3

Output

[1] 501 505 508 503 506 510 505 507 502 501 503 502 502 509 506 509 507 504
[19] 501 510 501 509 506 504 501 501 503 506 509 504 505 505 509 502 509 509
[37] 508 504 505 506 503 504 505 503 507 506 503 510 506 506 506 505 502 506
[55] 502 503 510 501 505 510 501 508 506 506 501 507 508 508 507 505 508 509
[73] 507 506 508 504 510 506 510 505 509 504 505 505 510 507 504 505 503 506
[91] 507 509 504 504 506 508 503 502 505 508 510 508 506 501 501 507 506 501
[109] 504 507 501 510 508 504 510 507 506 505 502 507 510 506 501 510 509 508
[127] 503 508 504 508 504 505 503 503 508 506 501 503 502 509 502 508 509 506
[145] 510 502 508 507 509 507 506 510 501 508 502 505 502 503 510 508 503 506
[163] 505 505 509 502 504 501 501 508 508 507 509 501 505 509 502 508 508 505
[181] 508 502 509 501 508 502 510 507 504 509 503 503 509 501 504 503 508 501
[199] 506 502

Example

Table3<−table(x3)
Table3

Output

x3
501 502 503 504 505 506 507 508 509 510
22 18 18 17 21 25 16 26 20 17
Table3[1]
501
22
Table3[5]
505
21
Table3[1:5]
x3
501 502 503 504 505
22 18 18 17 21
Table3[6:10]
x3
506 507 508 509 510
25 16 26 20 17
Updated on: 2021-02-10T07:20:17+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements