I have a vector I would like to repeat n times using the vector as columns in the new matrix
i.e I have a vector
vec <- c(266, 130, 86, 69, 56, 39, 30, 44, 33, 43)
vec
[1] 266 130 86 69 56 39 30 44 33 43
I would like to produce n times
vec1 vec1
266 266
130 130
86 86
69 69
56 56
39 39
30 30
44 44
33 33
43 43 .....
I am not entirely familiar with do.call but would you use that function to achieve this ?
cbind() and rbind() both create matrices by combining several vectors of the same length. cbind() combines vectors as columns, while rbind() combines them as rows.
cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns.
Data frame methodsThe cbind data frame method is just a wrapper for data. frame(..., check. names = FALSE) . This means that it will split matrix columns in data frame arguments, and convert character columns to factors unless stringsAsFactors = FALSE is specified.
How to Use cbind in R (With Examples) The cbind function in R, short for column-bind, can be used to combine vectors, matrices and data frames by column. The following examples show how to use this function in practice. Example 1: Cbind Vectors into a Matrix
We can join vectors by columns using cbind and it does not matter whether these vectors are of same length or not. If the vectors are of same length then all the values of both the vectors are printed but if the length of these vectors are different then the values of the smaller vector gets repeated.
To bind vectors, matrices, or data frames by rows in R, use the rbind () function. Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python.
We can use a vector of logical values to index another vector of the same length. R includes the elements corresponding to TRUE in the index vector and omits the elements corresponding to FALSE.
R recycles vectors when you create a matrix, so you can use:
matrix( vec , length(vec) , n )
where n
is the number of columns/repetitions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With