How to create a matrix from vector returned by rep() function?

x=1:20

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

rep(x,2)

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

View(rep(x,2))

Having a problem with generating a 20 by 2 vector using the rep() function in R.

Instead of creating two columns, each running from 1 to 20, when I view the data in the R workspace, it is displayed as 40X1 vector i.e. 1-20 1-20.

How do you use the rep() function to create a repeated column vector of 20X2? Thank you.

Answers


rep will return an atomic vector. If you want a matrix, use matrix on the results, with the appropriate dimensions.

eg.

x <- 1:20
matrix(rep(x,2), ncol = 2)
      [,1] [,2]
 [1,]    1    1
 [2,]    2    2
 [3,]    3    3
 [4,]    4    4
 [5,]    5    5
 [6,]    6    6
 [7,]    7    7
 [8,]    8    8
 [9,]    9    9
[10,]   10   10
[11,]   11   11
[12,]   12   12
[13,]   13   13
[14,]   14   14
[15,]   15   15
[16,]   16   16
[17,]   17   17
[18,]   18   18
[19,]   19   19
[20,]   20   20

Need Your Help

Javascript regex .test() "Uncaught TypeError: undefined is not a function"

javascript jquery regex

Just trying to use javascript's regex capabilities with the .test() function.

How can I get the class name from a C++ object?

c++ class

Is it possible to get the object name too?