How to create a matrix using vector of string values in R?

R ProgrammingServer Side ProgrammingProgramming

You might have heard that matrix can contain only numerical values but it is also possible to create a matrix with string values, and of course calculations using these types of matrices would not be possible. To create a matrix using string values, we can first create a vector of strings then define its dimension with dim function and that will convert the vector into matrix of string values.

Example

Live Demo

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

Output

 "A" "A" "A" "A" "A" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D" "D"
 "D" "E" "E" "E" "E" "E"

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

Output

[,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)  TRUE

Example

Live Demo

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

Output

 "C" "A" "B" "C" "B" "D" "A" "D" "A" "A" "D" "D" "A" "D" "C" "D" "D" "D" "C"
 "C" "C" "B" "A" "A" "D" "A" "C" "B" "D" "D" "C" "D" "D" "C" "C" "C"

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

Output

[,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)  TRUE

Example

Live Demo

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

Output

 "d" "a" "d" "b" "b" "c" "j" "d" "h" "f" "f" "b" "a" "i" "h" "i" "e" "h" "d"
 "a" "i" "g" "a" "g" "h" "h" "e" "g" "a" "g" "c" "i" "j" "d" "e" "f" "b" "b"
 "i" "d" "d" "e" "d" "e" "g" "g" "j" "g" "a" "g" "j" "j" "d" "c" "b" "j" "h"
 "f" "e" "a"

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

Output

[,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)  TRUE

Example

Live Demo

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

Output

 "J" "R" "D" "B" "Z" "F" "V" "M" "U" "M" "X" "A" "L" "L" "N" "G" "O" "H"
 "J" "C" "Y" "L" "I" "N" "Y" "D" "E" "N" "U" "Q" "Y" "X" "J" "O" "T" "Q"
 "X" "H" "V" "Z" "I" "P" "J" "Q" "Z" "G" "J" "V" "U" "P" "X" "P" "K" "O"
 "Y" "L" "E" "B" "B" "P" "Y" "D" "M" "S" "U" "K" "D" "N" "B" "B" "P" "V"
 "O" "Z" "L" "L" "G" "J" "Y" "Z" "K" "C" "Z" "Q" "Y" "C" "G" "H" "F" "J"
 "Q" "W" "Q" "H" "H" "Q" "O" "M" "I" "S"

Example

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

Output

[,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)  TRUE

Example

Live Demo

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

Output

 "China" "Denmark" "China" "China" "Russia" "Russia" "Denmark"
 "China" "Russia" "Russia" "Denmark" "India" "China" "Russia"
 "India" "China" "China" "China" "Russia" "India" "India"
 "Denmark" "Russia" "Russia" "Russia" "China" "China" "India"
 "Russia" "Denmark" "Russia" "Russia" "China" "India" "Russia"
 "Russia" "Russia" "India" "China" "Denmark" "Russia" "China"
 "India" "Russia" "China" "China" "China" "Russia" "India"
 "Denmark" "India" "China" "India" "Denmark" "Denmark" "Denmark"
 "Denmark" "India" "Russia" "China"

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

Output

[,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)  TRUE

Example

Live Demo

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

Output

 "Hot" "Cold" "Hot" "Winter" "Winter" "Hot" "Cold" "Winter"
 "Hot" "Winter" "Cold" "Winter" "Hot" "Winter" "Hot" "Winter"
 "Hot" "Winter" "Cold" "Cold" "Winter" "Cold" "Cold" "Hot"
 "Hot" "Hot" "Hot" "Hot" "Cold" "Winter" "Winter" "Cold"
 "Cold" "Winter" "Winter" "Hot" "Hot" "Hot" "Hot" "Hot"

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

Output

[,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)  TRUE