- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replicate a vector to create matrix in R?
The matrix can be created by using matrix function in R and if we want to create a matrix by replicating a vector then we just need to focus on the replication. For example, if we have a vector V and we want to create matrix by replicating V two times then the matrix can be created as matrix(replicate(2,V),nrow=2).
Example1
V1<-1:5 matrix(replicate(2,V1),nrow=5)
Output
[,1] [,2] [1,] 1 1 [2,] 2 2 [3,] 3 3 [4,] 4 4 [5,] 5 5
Example
matrix(replicate(5,V1),nrow=5)
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 2 2 2 2 2 [3,] 3 3 3 3 3 [4,] 4 4 4 4 4 [5,] 5 5 5 5 5
Example
matrix(replicate(10,V1),nrow=5)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 1 1 1 1 1 1 1 1 1 [2,] 2 2 2 2 2 2 2 2 2 2 [3,] 3 3 3 3 3 3 3 3 3 3 [4,] 4 4 4 4 4 4 4 4 4 4 [5,] 5 5 5 5 5 5 5 5 5 5
Example
matrix(replicate(9,V1),nrow=5)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 1 1 1 1 1 1 1 1 [2,] 2 2 2 2 2 2 2 2 2 [3,] 3 3 3 3 3 3 3 3 3 [4,] 4 4 4 4 4 4 4 4 4 [5,] 5 5 5 5 5 5 5 5 5
Example2
V2<-rpois(10,5) V2
Output
[1] 1 4 2 6 7 2 4 4 2 8
Example
matrix(replicate(2,V2),nrow=10)
Output
[,1] [,2] [1,] 1 1 [2,] 4 4 [3,] 2 2 [4,] 6 6 [5,] 7 7 [6,] 2 2 [7,] 4 4 [8,] 4 4 [9,] 2 2 [10,] 8 8
Example
matrix(replicate(5,V2),nrow=10)
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 4 4 4 4 4 [3,] 2 2 2 2 2 [4,] 6 6 6 6 6 [5,] 7 7 7 7 7 [6,] 2 2 2 2 2 [7,] 4 4 4 4 4 [8,] 4 4 4 4 4 [9,] 2 2 2 2 2 [10,] 8 8 8 8 8
Example
matrix(replicate(10,V2),nrow=10)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 1 1 1 1 1 1 1 1 1 [2,] 4 4 4 4 4 4 4 4 4 4 [3,] 2 2 2 2 2 2 2 2 2 2 [4,] 6 6 6 6 6 6 6 6 6 6 [5,] 7 7 7 7 7 7 7 7 7 7 [6,] 2 2 2 2 2 2 2 2 2 2 [7,] 4 4 4 4 4 4 4 4 4 4 [8,] 4 4 4 4 4 4 4 4 4 4 [9,] 2 2 2 2 2 2 2 2 2 2 [10,] 8 8 8 8 8 8 8 8 8 8
Example3
V3<-sample(0:9,10,replace=TRUE) V3
Output
[1] 1 8 8 5 9 7 0 7 1 8 matrix(replicate(5,V3),nrow=10)
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 8 8 8 8 8 [3,] 8 8 8 8 8 [4,] 5 5 5 5 5 [5,] 9 9 9 9 9 [6,] 7 7 7 7 7 [7,] 0 0 0 0 0 [8,] 7 7 7 7 7 [9,] 1 1 1 1 1 [10,] 8 8 8 8 8
Example
matrix(replicate(3,V3),nrow=10)
Output
[,1] [,2] [,3] [1,] 1 1 1 [2,] 8 8 8 [3,] 8 8 8 [4,] 5 5 5 [5,] 9 9 9 [6,] 7 7 7 [7,] 0 0 0 [8,] 7 7 7 [9,] 1 1 1 [10,] 8 8 8
Example
matrix(replicate(12,V3),nrow=10)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 1 1 1 1 1 1 1 1 1 1 1 1 [2,] 8 8 8 8 8 8 8 8 8 8 8 8 [3,] 8 8 8 8 8 8 8 8 8 8 8 8 [4,] 5 5 5 5 5 5 5 5 5 5 5 5 [5,] 9 9 9 9 9 9 9 9 9 9 9 9 [6,] 7 7 7 7 7 7 7 7 7 7 7 7 [7,] 0 0 0 0 0 0 0 0 0 0 0 0 [8,] 7 7 7 7 7 7 7 7 7 7 7 7 [9,] 1 1 1 1 1 1 1 1 1 1 1 1 [10,] 8 8 8 8 8 8 8 8 8 8 8 8
Advertisements