Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat non-integer values in a sequence

Tags:

r

I want to create a sequence of non-integer values where each value is repeated a certain number of times in R. I've tried combining variations of:

seq(5,0,by=-0.5)

which returns

# [1] 5.0 4.5 4.0 3.5 3.0 2.5 2.0 1.5 1.0 0.5 0.0

as well as

rep(5:0,each=3)

which returns

# [1] 5 5 5 4 4 4 3 3 3 2 2 2 1 1 1 0 0 0

each of which contains partly what I want, which would look like this:

5 5 5 4.5 4.5 4.5 4 4 4 3.5 3.5 3.5 3 3 3 2.5 2.5 2.5 2 2 2 1.5 1.5 1.5 1 1 1 0.5 0.5 0.5 0 0 0

It seems that seq() contains no "each" analogue, whereas rep() contains no "by" argument. Anyone know of a third function that can do both, or a way to combine these two to accomplish my goal? Or, another way alltogether?

like image 862
Andrew Durso Avatar asked Jun 08 '26 18:06

Andrew Durso


1 Answers

Just combine!

rep(seq(5,0,by=-0.5), each=3)
like image 171
jalapic Avatar answered Jun 11 '26 08:06

jalapic