Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than one value for "each" argument in "rep" function?

Tags:

r

rep

How to assign more than one value for "each" argument in "rep" function in R? A trivial example, where each value in a vector is 3-times repeated in a row:

a <- seq(2,6,2)
rep (a,each = 3)

However, if I add more than one value in "each" argument in order to change the number of repetition of each value, it doesn't work properly:

rep (a, each = c(2,4,7))

How to solve it? Thank you in advance.

like image 997
user36478 Avatar asked May 22 '14 22:05

user36478


People also ask

What does the rep () function do in R?

What is the rep() function? In simple terms, rep in R, or the rep() function replicates numeric values, or text, or the values of a vector for a specific number of times.

How do you repeat a number in R?

How do you Repeat a Sequence of Numbers in R? To repeat a sequence of numbers in R you can use the rep() function. For example, if you type rep(1:5, times=5) you will get a vector with the sequence 1 to 5 repeated 5 times.

How do you repeat a value in a vector 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.


1 Answers

Depending on what you think the output should be, I'm guessing you want the times= parameter:

rep (a, times = c(2, 4, 7))
# [1] 2 2 4 4 4 4 6 6 6 6 6 6 6

See ?rep for the difference

like image 124
MrFlick Avatar answered Oct 19 '22 11:10

MrFlick