R: seq() and rep()

If you want to count out a sequence of number in R, you can simply use a :

1:15 goes from 1 to 15, while 6:22 goes from 6 to 22

seqRep.jpg

You can even assign these sequences to a variable, creating a vector

seqRep1.jpg

However, you can only count by 1 using this method.

seq()

With seq(1,15) I can count from 1 to 15, just like using 1:15

seqRep2.jpg

If I add a third argument though, now I am counting from 1 to 15 by 4

seqRep3.jpg

rep()

rep() stands for replicate. Using rep(), I can make a list of repeating elements

seqRep4

You can of course create vectors with both seq() and rep() by assigning them to a variable

seqRep5.jpg

Remember R indexes start at 1 not 0

so using the vector above, a[3] = 9 – a[1:3] = 1,5,9

 

 

 

Leave a Reply