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


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


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


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


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



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


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


Remember R indexes start at 1 not 0

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




Please Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s