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
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 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.
c() function in R Language is used to combine the arguments passed to it.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With