Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Vector of Length N of the same numbers in R

Tags:

r

I need a numeric vector with 100 4.5s inside it.

Is there a way to create this vector without typing out c(4.5, 4.5, .....) 100 times? Thank you.

like image 403
Axioms Avatar asked Mar 22 '18 17:03

Axioms


People also ask

How do I create a vector of the same number in R?

There are two methods to create a vector with repeated values in R but both of them have different approaches, first one is by repeating each element of the vector and the second repeats the elements by a specified number of times. Both of these methods use rep function to create the vectors.

How do I make a vector a specific length in R?

To create a vector of specified data type and length in R we make use of function vector(). vector() function is also used to create empty vector.

How do I create a vector of one in R?

Using rep() function It creates a vector with the value x repeated t times. To create a vector of ones, pass 1 as the first argument to the rep() function and the length of the vector as its second argument.

How do you repeat a value in R?

In R, the easiest way to repeat rows is with the REP() function. This function selects one or more observations from a data frame and creates one or more copies of them. Alternatively, you can use the SLICE() function from the dplyr package to repeat rows.


1 Answers

rep(4.5, 100)

The function rep does the trick

like image 139
Seymour Avatar answered Oct 04 '22 22:10

Seymour