Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate sequence with alternating increments in R? [duplicate]

Tags:

list

r

sequence

I want to use R to create the sequence of numbers 1:8, 11:18, 21:28, etc. through 1000 (or the closest it can get, i.e. 998). Obviously typing that all out would be tedious, but since the sequence increases by one 7 times and then jumps by 3 I'm not sure what function I could use to achieve this.

I tried seq(1, 998, c(1,1,1,1,1,1,1,3)) but it does not give me the results I am looking for so I must be doing something wrong.

like image 329
CIM Avatar asked Jan 25 '17 04:01

CIM


People also ask

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.

Which function is used to generate sequences in R?

seq() function in R Language is used to create a sequence of elements in a Vector.

What does length out mean in R?

out equally spaced values from from to to . ( length. out is usually abbreviated to length or len , and seq_len is much faster.) The fourth form generates the integer sequence 1, 2, ..., length(along. with) .


2 Answers

This is a perfect case of vectorisation( recycling too) in R. read about them

(1:100)[rep(c(TRUE,FALSE), c(8,2))]
# [1]  1  2  3  4  5  6  7  8 11 12 13 14 15 16 17 18 21 22 23 24 25 26 27 28 31 32
#[27] 33 34 35 36 37 38 41 42 43 44 45 46 47 48 51 52 53 54 55 56 57 58 61 62 63 64
#[53] 65 66 67 68 71 72 73 74 75 76 77 78 81 82 83 84 85 86 87 88 91 92 93 94 95 96
#[79] 97 98
like image 125
joel.wilson Avatar answered Oct 23 '22 13:10

joel.wilson


rep(seq(0,990,by=10), each=8) + seq(1,8)
like image 7
Jean Avatar answered Oct 23 '22 15:10

Jean