# How to create frequency table of a string vector in R?

To create a frequency table of a string vector, we just need to use table function. For example, if we have a vector x that contains randomly sampled 100 values of first five English alphabets then the table of vector x can be created by using table(x). This will generate a table along with the name of the vector.

## Example1

Live Demo

> x1<-sample(letters[1:4],20,replace=TRUE)
> x1

## Output

[1] "d" "d" "a" "c" "a" "a" "c" "a" "d" "c" "a" "d" "d" "b" "c" "a" "b" "c" "d"
[20] "b"

## Example

> table(x1)

## Output

x1
a b c d
6 3 5 6

## Example2

Live Demo

> x2<-sample(letters[1:26],120,replace=TRUE)
> x2

## Output

[1] "w" "j" "p" "y" "r" "m" "y" "r" "z" "v" "p" "d" "s" "x" "j" "t" "d" "e"
[19] "l" "m" "f" "p" "u" "a" "d" "y" "y" "k" "n" "i" "m" "g" "s" "e" "n" "a"
[37] "w" "a" "s" "w" "n" "f" "f" "n" "s" "q" "y" "a" "h" "d" "q" "h" "g" "f"
[55] "z" "e" "a" "v" "f" "a" "w" "o" "u" "c" "l" "h" "z" "o" "e" "w" "x" "t"
[73] "y" "f" "q" "e" "d" "c" "l" "s" "x" "i" "i" "q" "p" "o" "v" "k" "b" "w"
[91] "s" "k" "s" "l" "f" "t" "j" "u" "j" "s" "p" "w" "w" "o" "d" "x" "l" "h"
[109] "u" "d" "p" "l" "h" "s" "n" "a" "o" "k" "y" "m"

## Example

> table(x2)

## Output

x2
a b c d e f g h i j k l m n o p q r s t u v w x y z
7 1 2 7 5 7 2 5 3 4 4 6 4 5 5 6 4 2 9 3 4 3 8 4 7 3

## Example3

Live Demo

> x3<-sample(LETTERS[1:26],150,replace=TRUE)
> x3

## Output

[1] "U" "M" "A" "J" "Q" "L" "L" "Y" "A" "Q" "P" "R" "M" "T" "U" "H" "V" "H"
[19] "I" "H" "G" "D" "H" "V" "X" "K" "R" "H" "Y" "I" "L" "K" "O" "W" "Z" "K"
[37] "Q" "R" "D" "E" "I" "A" "E" "U" "C" "N" "S" "R" "O" "A" "Y" "I" "E" "E"
[55] "D" "F" "A" "G" "S" "Y" "B" "X" "H" "J" "O" "L" "A" "K" "D" "U" "N" "K"
[73] "S" "Y" "V" "V" "X" "Q" "M" "S" "G" "L" "I" "Y" "C" "T" "N" "I" "E" "E"
[91] "X" "G" "B" "Q" "D" "C" "G" "R" "P" "A" "Z" "A" "Z" "Z" "X" "G" "D" "G"
[109] "T" "T" "W" "I" "N" "H" "E" "P" "M" "U" "Q" "U" "P" "S" "N" "Z" "G" "P"
[127] "N" "I" "C" "H" "S" "U" "Q" "Q" "S" "T" "D" "D" "I" "S" "S" "V" "R" "Q"
[145] "V" "C" "O" "B" "E" "X"

## Example

> table(x3)

## Output

x3
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
8 3 5 8 8 1 8 8 9 2 5 5 4 6 4 5 9 6 9 5 7 6 2 6 6 5

## Example4

Live Demo

> x4<-sample(c("India","USA","UK","Turkey"),100,replace=TRUE)
> x4

## Output

[1] "Turkey" "India" "India" "USA" "India" "Turkey" "India" "USA"
[9] "India" "USA" "USA" "UK" "India" "UK" "USA" "India"
[17] "Turkey" "Turkey" "USA" "India" "Turkey" "USA" "USA" "UK"
[25] "USA" "Turkey" "India" "Turkey" "India" "UK" "UK" "UK"
[33] "UK" "USA" "USA" "UK" "Turkey" "USA" "India" "UK"
[41] "India" "India" "India" "Turkey" "USA" "India" "USA" "USA"
[49] "India" "UK" "India" "UK" "USA" "Turkey" "Turkey" "USA"
[57] "Turkey" "Turkey" "India" "Turkey" "Turkey" "UK" "Turkey" "USA"
[65] "Turkey" "India" "Turkey" "USA" "USA" "UK" "UK" "Turkey"
[73] "USA" "India" "Turkey" "Turkey" "India" "Turkey" "USA" "UK"
[81] "India" "Turkey" "USA" "UK" "UK" "UK" "USA" "USA"
[89] "India" "India" "Turkey" "Turkey" "USA" "Turkey" "Turkey" "UK"
[97] "USA" "UK" "UK" "UK"

## Example

> table(x4)

## Output

x4
India Turkey UK USA
24 27 22 27

## Example5

Live Demo

> x5<-sample(c("Europe","Asia","North America","Africa","South America","Antartica","Oceania"),100,replace=TRUE)
> x5

## Output

[1] "Asia" "Oceania" "Antartica" "Antartica"
[5] "South America" "Oceania" "Africa" "Africa"
[9] "Europe" "Africa" "Africa" "North America"
[13] "North America" "Oceania" "Antartica" "Asia"
[17] "Antartica" "Europe" "Asia" "North America"
[21] "South America" "Europe" "Oceania" "Oceania"
[25] "Asia" "Oceania" "South America" "Antartica"
[29] "Europe" "Europe" "Oceania" "Africa"
[33] "Asia" "Africa" "South America" "Antartica"
[37] "North America" "South America" "South America" "South America"
[41] "Asia" "South America" "Europe" "South America"
[45] "Asia" "North America" "Africa" "North America"
[49] "North America" "South America" "North America" "Antartica"
[53] "Asia" "South America" "North America" "South America"
[57] "Antartica" "South America" "Asia" "North America"
[61] "North America" "Oceania" "North America" "Asia"
[65] "Europe" "Oceania" "Africa" "Antartica"
[69] "Antartica" "Antartica" "North America" "Asia"
[73] "Antartica" "Antartica" "South America" "Europe"
[77] "Africa" "North America" "Asia" "Africa"
[81] "Oceania" "Africa" "Europe" "Asia"
[85] "North America" "Africa" "Europe" "Oceania"
[89] "Asia" "Europe" "Europe" "Africa"
[93] "Africa" "Antartica" "Antartica" "Africa"
[97] "South America" "Oceania" "South America" "North America"

## Example

> table(x5)

## Output

x5
Africa Antartica Asia Europe North America
15 15 14 12 16
Oceania South America
12 16

Updated on: 19-Nov-2020

855 Views