Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create vectors with specific intervals in R?

I have a question about creating vectors. If I do a <- 1:10, "a" has the values 1,2,3,4,5,6,7,8,9,10.

My question is how do you create a vector with specific intervals between its elements. For example, I would like to create a vector that has the values from 1 to 100 but only count in intervals of 5 so that I get a vector that has the values 5,10,15,20,...,95,100

I think that in Matlab we can do 1:5:100, how do we do this using R?

I could try doing 5*(1:20) but is there a shorter way? (since in this case I would need to know the whole length (100) and then divide by the size of the interval (5) to get the 20)

like image 502
Luli Avatar asked Mar 24 '13 17:03

Luli


People also ask

How do I create a vector of a specific length in R?

To create a vector of specified data type and length in R we make use of function vector(). vector() function is also used to create empty vector.

How do I create a repeating value vector in R?

There are two methods to create a vector with repeated values in R but both of them have different approaches, first one is by repeating each element of the vector and the second repeats the elements by a specified number of times. Both of these methods use rep function to create the vectors.

How do you create a vector in R with example?

Vectors in R are the same as the arrays in C language which are used to hold multiple data values of the same type. One major key point is that in R the indexing of the vector will start from '1' and not from '0'. We can create numeric vectors and character vectors as well.


2 Answers

In R the equivalent function is seq and you can use it with the option by:

seq(from = 5, to = 100, by = 5) # [1]   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100 

In addition to by you can also have other options such as length.out and along.with.

length.out: If you want to get a total of 10 numbers between 0 and 1, for example:

seq(0, 1, length.out = 10) # gives 10 equally spaced numbers from 0 to 1 

along.with: It takes the length of the vector you supply as input and provides a vector from 1:length(input).

seq(along.with=c(10,20,30)) # [1] 1 2 3 

Although, instead of using the along.with option, it is recommended to use seq_along in this case. From the documentation for ?seq

seq is generic, and only the default method is described here. Note that it dispatches on the class of the first argument irrespective of argument names. This can have unintended consequences if it is called with just one argument intending this to be taken as along.with: it is much better to use seq_along in that case.

seq_along: Instead of seq(along.with(.))

seq_along(c(10,20,30)) # [1] 1 2 3 

Hope this helps.

like image 193
Arun Avatar answered Sep 28 '22 07:09

Arun


Use the code

x = seq(0,100,5) #this means (starting number, ending number, interval) 

the output will be

[1]   0   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75 [17]  80  85  90  95 100 
like image 43
sumit Avatar answered Sep 28 '22 07:09

sumit