如何使用R中的字符串值向量创建矩阵?

您可能听说过矩阵只能包含数字值,但是也可以创建带有字符串值的矩阵,并且当然不可能使用这些类型的矩阵进行计算。要使用字符串值创建矩阵,我们可以先创建一个字符串向量,然后使用dim函数定义维数,然后将向量转换为字符串值矩阵。

示例

M1<-rep(c("A","B","C","D","E"),each=5)

M1

输出结果

[1] "A" "A" "A" "A" "A" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D" "D"

[20] "D" "E" "E" "E" "E" "E"

示例

dim(M1)<-c(5,5)

M1

输出结果

    [,1] [,2] [,3] [,4] [,5] 

[1,] "A" "B"  "C"  "D"  "E" 

[2,] "A" "B"  "C"  "D"  "E" 

[3,] "A" "B"  "C"  "D"  "E" 

[4,] "A" "B"  "C"  "D"  "E" 

[5,] "A" "B"  "C"  "D"  "E"

is.matrix(M1)[1]是

示例

M2<-sample(c("A","B","C","D"),36,replace=TRUE)

M2

输出结果

[1] "C" "A" "B" "C" "B" "D" "A" "D" "A" "A" "D" "D" "A" "D" "C" "D" "D" "D" "C"

[20] "C" "C" "B" "A" "A" "D" "A" "C" "B" "D" "D" "C" "D" "D" "C" "C" "C"

示例

dim(M2)<-c(6,6)

M2

输出结果

    [,1] [,2] [,3] [,4] [,5] [,6] 

[1,] "D" "A"  "C"  "A"  "B"  "C" 

[2,] "D" "B"  "B"  "D"  "A"  "B" 

[3,] "C" "B"  "B"  "A"  "D"  "C" 

[4,] "D" "D"  "C"  "B"  "A"  "B" 

[5,] "C" "B"  "A"  "A"  "D"  "A" 

[6,] "A" "B"  "B"  "A"  "A"  "D"

is.matrix(M2)[1]是

示例

M3<-sample(letters[1:10],60,replace=TRUE) M3

输出结果

[1] "d" "a" "d" "b" "b" "c" "j" "d" "h" "f" "f" "b" "a" "i" "h" "i" "e" "h" "d"

[20] "a" "i" "g" "a" "g" "h" "h" "e" "g" "a" "g" "c" "i" "j" "d" "e" "f" "b" "b"

[39] "i" "d" "d" "e" "d" "e" "g" "g" "j" "g" "a" "g" "j" "j" "d" "c" "b" "j" "h"

[58] "f" "e" "a"

示例

dim(M3)<-c(10,6)

M3

输出结果

    [,1] [,2]  [,3] [,4]  [,5] [,6]

[1,] "f"  "h"  "a"   "f"  "d"  "c" 

[2,] "e"  "h"  "g"   "b"  "i"  "e"

[3,] "b"  "j"  "h"   "h"  "h"  "g"

[4,] "i"  "f"  "d"   "i"  "c"  "d"

[5,] "j"  "f"  "j"   "b"  "j"  "d" 

[6,] "a"  "d"  "c"   "j"  "e"  "j" 

[7,] "b"  "i"  "j"   "i"  "e"  "c" 

[8,] "c"  "i"  "d"   "i"  "b"  "a" 

[9,] "g"  "f"  "a"   "d"  "d"  "j" 

[10,] "d" "f"  "e"   "b"  "f"  "i"

is.matrix(M3)[1]是

示例

M4<-sample(LETTERS[1:26],100,replace=TRUE)M4

输出结果

[1] "J" "R" "D" "B" "Z" "F" "V" "M" "U" "M" "X" "A" "L" "L" "N" "G" "O" "H"

[19] "J" "C" "Y" "L" "I" "N" "Y" "D" "E" "N" "U" "Q" "Y" "X" "J" "O" "T" "Q"

[37] "X" "H" "V" "Z" "I" "P" "J" "Q" "Z" "G" "J" "V" "U" "P" "X" "P" "K" "O"

[55] "Y" "L" "E" "B" "B" "P" "Y" "D" "M" "S" "U" "K" "D" "N" "B" "B" "P" "V"

[73] "O" "Z" "L" "L" "G" "J" "Y" "Z" "K" "C" "Z" "Q" "Y" "C" "G" "H" "F" "J"

[91] "Q" "W" "Q" "H" "H" "Q" "O" "M" "I" "S"

示例

dim(M4)<-c(10,10)

M4

输出结果

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

[1,] "I"  "K"  "A"   "X"  "X"  "N"  "Y"  "U"  "U"  "P"

[2,] "Y"  "R"  "Q"   "Y"  "V"  "M"  "T"  "L"  "X"  "X" 

[3,] "F"  "H"  "E"   "N"  "V"  "G"  "A"  "G"  "Y"  "P" 

[4,] "C"  "D"  "K"   "E"  "P"  "M"  "S"  "B"  "K"  "Z" 

[5,] "W"  "C"  "G"   "H"  "J"  "E"  "U"  "G"  "C"  "Q" 

[6,] "D"  "T"  "J"   "V"  "D"  "A"  "L"  "S"  "I"  "P" 

[7,] "N"  "T"  "W"   "T"  "J"  "T"  "R"  "E"  "S"  "S" 

[8,] "W"  "N"  "P"   "O"  "B"  "E"  "M"  "W"  "G"  "P" 

[9,] "I"  "I"  "N"   "M"  "I"  "L"  "W"  "E"  "D"  "W" 

[10,] "E"  "Y"  "B"  "T"  "M"  "C"  "F"  "P"  "R"  "L"

is.matrix(M4)[1]是

示例

M5<-sample(c("India","Russia","China","Denmark"),60,replace=TRUE)

M5

输出结果

[1] "China" "Denmark" "China" "China" "Russia" "Russia" "Denmark"

[8] "China" "Russia" "Russia" "Denmark" "India" "China" "Russia"

[15] "India" "China" "China" "China" "Russia" "India" "India"

[22] "Denmark" "Russia" "Russia" "Russia" "China" "China" "India"

[29] "Russia" "Denmark" "Russia" "Russia" "China" "India" "Russia"

[36] "Russia" "Russia" "India" "China" "Denmark" "Russia" "China"

[43] "India" "Russia" "China" "China" "China" "Russia" "India"

[50] "Denmark" "India" "China" "India" "Denmark" "Denmark" "Denmark"

[57] "Denmark" "India" "Russia" "China"

示例

dim(M5)<-c(10,6)

M5

输出结果

        [,1]    [,2]    [,3]       [,4]       [,5]       [,6] 

[1,] "India"  "China"   "India"  "Russia"  "Denmark"   "Denmark" 

[2,] "Russia"  "Denmark"  "Russia" "Russia" "Denmark"   "India" 

[3,] "China"  "India"   "China"    "China"  "Denmark"   "Denmark"

 [4,] "Denmark"  "India"  "Russia"  "India" "Denmark"   "China" 

[5,] "Russia"  "China"  "Russia"  "China"   "Russia"   "China" 

[6,] "Denmark"  "China"  "Russia"  "Denmark" "Denmark"  "China"

 [7,] "Denmark"  "Russia"  "China"  "India"  "China"   "China" 

[8,] "Denmark"  "China"  "Denmark"  "India"  "India"   "Denmark" 

[9,] "Denmark"  "India"  "Denmark"  "India"  "Russia"   "China" 

[10,] "Russia"  "Denmark" "China"  "India"  "Denmark"  "India"

is.matrix(M5)[1]是

示例

M6<-sample(c("Cold","Winter","Hot"),40,replace=TRUE)

M6

输出结果

[1] "Hot" "Cold" "Hot" "Winter" "Winter" "Hot" "Cold" "Winter"

[9] "Hot" "Winter" "Cold" "Winter" "Hot" "Winter" "Hot" "Winter"

[17] "Hot" "Winter" "Cold" "Cold" "Winter" "Cold" "Cold" "Hot"

[25] "Hot" "Hot" "Hot" "Hot" "Cold" "Winter" "Winter" "Cold"

[33] "Cold" "Winter" "Winter" "Hot" "Hot" "Hot" "Hot" "Hot"

示例

dim(M6)<-c(10,4)

M6

输出结果

     [,1]     [,2]      [,3]      [,4] 

[1,] "Hot"    "Hot"    "Cold"    "Winter"

[2,] "Hot"    "Hot"     "Winter"  "Winter"

[3,] "Winter" "Cold"  "Winter"  "Cold" 

[4,] "Cold"  "Cold"   "Hot"    "Winter"

[5,] "Hot"   "Cold"    "Cold"    "Hot" 

[6,] "Cold"  "Winter"  "Hot"    "Cold" 

[7,] "Winter" "Winter" "Cold"   "Hot" 

[8,] "Cold"  "Cold"  "Cold"    "Hot"

[9,] "Cold"  "Cold"  "Winter"  "Cold" 

[10,] "Cold" "Winter" "Winter" "Hot"

is.matrix(M6)[1]是

以上是 如何使用R中的字符串值向量创建矩阵? 的全部内容, 来源链接: utcz.com/z/345469.html

回到顶部