Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cbind a vector multiple times in R

Tags:

r

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 ?

like image 570
user124123 Avatar asked Feb 26 '16 16:02

user124123


People also ask

What is the use of Rbind () and Cbind () in R?

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.

What does Cbind function in R do?

cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns.

Does Cbind create Dataframe?

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.

What is cbind in R?

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

Can We join two vectors of the same length using cbind?

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.

How to bind vectors by R R R?

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.

How to index a vector of the same length in R?

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.


1 Answers

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.

like image 102
David Heckmann Avatar answered Oct 12 '22 21:10

David Heckmann