Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a repeating sequence based on vector

Tags:

r

repeat

I am trying to take an existing vector and repeat each element of it six times. I feel like this should be easy using rep() but I keep hitting the wall. Basically I would like to take this vector:

1027 1028 1030 1032 1037 

And turn it into this:

1027 1027 1027 1027 1027 1027 1028 1028 1028 1028 1028 1028 ... 
like image 619
Stedy Avatar asked Sep 08 '10 22:09

Stedy


People also ask

How do I create a repeating value 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.

How do you make a repeating sequence 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.


1 Answers

Use each argument:

rep(c(1027, 1028, 1030, 1032, 1037), each = 6) #  [1] 1027 1027 1027 1027 1027 1027 #  [7] 1028 1028 1028 1028 1028 1028 # [13] 1030 1030 1030 1030 1030 1030 # [19] 1032 1032 1032 1032 1032 1032 # [25] 1037 1037 1037 1037 1037 1037 

times argument:

rep(c(1027, 1028, 1030, 1032, 1037), times = 6) #  [1] 1027 1028 1030 1032 1037 #  [6] 1027 1028 1030 1032 1037 # [11] 1027 1028 1030 1032 1037 # [16] 1027 1028 1030 1032 1037 # [21] 1027 1028 1030 1032 1037 # [26] 1027 1028 1030 1032 1037 
like image 64
mbq Avatar answered Oct 07 '22 23:10

mbq