如何在R中创建字符串向量的频率表?

要创建一个字符串向量的频率表,我们只需要使用表函数。例如,如果我们有一个向量x,其中包含随机采样的前五个英文字母的100个值,则可以使用table(x)创建向量x的表。这将生成一个表格以及向量名称。

例1

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

输出结果

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

示例

> table(x1)

输出结果

x1
a b c d
6 3 5 6

例2

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

输出结果

[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"

示例

> table(x2)

输出结果

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

例子3

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

输出结果

[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"

示例

> table(x3)

输出结果

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

例子4

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

输出结果

[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"

示例

> table(x4)

输出结果

x4
India Turkey UK USA
24 27 22 27

范例5

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

输出结果

[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"

示例

> table(x5)

输出结果

x5
Africa Antartica Asia Europe North America
15 15 14 12 16
Oceania South America
12 16
猜你喜欢