Reputation: 751
I recall a function in (base?) R that can generate a matrix that looks like:
structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
4L, 5L, 1L, 2L, 3L, 4L, 5L), .Dim = c(5L, 4L))
Displayed as:
>
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
[4,] 4 4 4 4
[5,] 5 5 5 5
Or also the column form:
>
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
[4,] 1 2 3 4
[5,] 1 2 3 4
But I've completely forgotten what the command was, or even what to search for... can someone please remind me.
And generally the functions to generate matrices, like diag() for example, other than using matrix().
Upvotes: 0
Views: 119
Reputation: 9687
row()
and col()
take a matrix and return a new one of the same shape with the structure you describe.
Upvotes: 6
Reputation: 1643
Here are two lines of code that create both matrices you wrote, I am not sure if you are looking for something more general though.
matrix(rep(1:5,4),ncol=4)
matrix(rep(1:4,5),ncol=4,byrow=T)
Upvotes: 3