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.
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.
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.
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.
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.
rep(4.5, 100)
The function rep does the trick
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