Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a list with a specified increment step?

Tags:

r

seq

How do I generate a vector with a specified increment step (e.g. 2)? For example, how do I produce the following

0  2  4  6  8  10 
like image 743
LostLin Avatar asked Sep 07 '11 21:09

LostLin


People also ask

How do I generate a list of numbers in R?

How to Create Lists in R? We can use the list() function to create a list. Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.

How to repeat a series of numbers 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.

What is C () R?

c() function in R Language is used to combine the arguments passed to it.


2 Answers

Executing seq(1, 10, 1) does what 1:10 does. You can change the last parameter of seq, i.e. by, to be the step of whatever size you like.

> #a vector of even numbers > seq(0, 10, by=2) # Explicitly specifying "by" only to increase readability  > [1]  0  2  4  6  8 10 
like image 92
John Avatar answered Oct 07 '22 04:10

John


You can use scalar multiplication to modify each element in your vector.

> r <- 0:10  > r <- r * 2 > r   [1]  0  2  4  6  8 10 12 14 16 18 20 

or

> r <- 0:10 * 2  > r   [1]  0  2  4  6  8 10 12 14 16 18 20 
like image 36
Travis Nelson Avatar answered Oct 07 '22 06:10

Travis Nelson