# seq Function in R (5 Examples)

In this tutorial youâ€™ll learn how to **generate a sequence of numbers using the seq function** in the R programming language.

The article looks as follows:

Letâ€™s get started.

## Definition & Basic R Syntax of seq Function
##

**Definition:** The seq R function generates a sequence of numeric values.

**Basic R Syntax:** You can find the basic R programming syntax of the seq function below.

seq(1, 5) # Basic R syntax of seq function

In the following, Iâ€™ll show five examples for the application of the seq function in R programming.

## Example 1: Basic Application of seq() Function

The following code illustrates how to apply the seq function in its most minimalistic way:

seq(10) # Apply seq function # 1 2 3 4 5 6 7 8 9 10

In the previous R code, we simply specified the value 10 within the seq function. In this case, the function uses the value 1 as starting point and the value 10 as ending point of the sequence. As you can see based on the previous R code, we therefore created a numeric vector ranging from 1 to 10.

## Example 2: Generating Sequence from X to Y

In this Section, Iâ€™ll show how to modify the starting point of our sequence using the from argument of the seq function.

seq(3, 10) # seq function from to # 3 4 5 6 7 8 9 10

In the previous R syntax, we specified two values. The first value is the starting point (i.e. the from argument) and the second value is the ending point (i.e. the to argument).

## Example 3: Generating Sequence from X to Y by Certain Value

In this Example, Iâ€™ll explain how to create a sequence with an increment unequal to 1. For this, we have to specify a third value within the seq function (i.e. the by argument):

seq(3, 10, 0.5) # seq function by 0.5 # 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0

The previous R code generated a sequence with increment 0.33.

## Example 4: Generating Sequence from X to Y with Given Length

In Example 4, Iâ€™ll explain how to return a sequence of numeric values with a manually specified length using the length.out argument. In this example, we specify that the length of our vector should be 5:

seq(3, 10, length.out = 5) # Sequence of length 5 # 3.00 4.75 6.50 8.25 10.00

## Example 5: Generating Sequence from X to Y Along Certain Data Object

Similar to the length.out argument shown in Example 4, we can also use the along.with argument. The along.with argument specifies that the output of the seq function should have the same length as the data object assigned to along.with.

seq(3, 10, along.with = 1:5) # Using along.with argument # 3.00 4.75 6.50 8.25 10.00

## Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which shows the examples of this post. Please find the video below:

In addition to the video, you may want to have a look at some of the other articles of my website. I have released several articles already.

- Create Sequence of Repeated Values
- rep Function in R
- R pretty Function
- R Functions List (+ Examples)
- The R Programming Language

In summary: This article showed how to **apply the seq function** in the R programming language.

Note that the R programming language provides some primitive alternatives to the seq function, which can be used to improve the speed and efficiency of a code. These functions include seq.int(), seq_along(), and seq_len().

Donâ€™t hesitate to let me know in the comments below, in case you have any additional questions and/or comments. In addition, donâ€™t forget to subscribe to my email newsletter in order to get updates on new articles.

## 24 Comments. Leave new

hi i have a problem in R code, can u halp me thanx

Rashid Ali

from karachi Pakistan.

Hey Rashid,

Could you post your question here in the comments?

Regards,

Joachim

Hi Joachim

How to create a vector using seq function if the size of the vector given is 10 and the elements in

the vector are multiples of 5 arranged sequentially, starting from 40?

Hey Ghotala,

Do you mean like that?

Regards,

Joachim

Hi please help me with this question,

# Try to write a code for printing sequence of numbers from 1 to 50 with the differences of 3, 5, 10

Hey Ayesha,

Could you share the code you have tried yourself so far?

I’m happy to help, but I want to avoid doing your entire assignment. ðŸ™‚

Regards,

Joachim

I would like some help with my R code. How does one use seq() or rep() in creating the following vector in R. 8.0, 7.5, 7.0, 6.5, 6.0, 5.5, 5.0, 4.5, 4.0, 3.5, 3.0, 2.5, 2.0

Hey Stephen,

Apologies for the late reply, I just got back from vacation. Do you still need help with this?

Regards,

Joachim

you can use , seq(from = 7.5, to = 2, by = -0.5)

you can user this code

seq(from = 8, to = 2, length.out = 13)

[1] 8.0 7.5 7.0 6.5 6.0 5.5 5.0 4.5 4.0 3.5 3.0 2.5 2.0

hi

i typed this code: seq(c(4,3,4,4,5) and got the output as #1 2 3 4 5 6

can you explain the logic of getting this

Hi Tharu,

I’m sorry for the late response, I just came back from holidays. Are you still looking for help?

Regards,

Joachim

How about : inside of seq()?

i.e. seq(4:12)

Hey Sookja,

Could you illustrate your desired output?

Regards,

Joachim

3. Create Data frame with 3 columns and 5 rows and write a code to fetch and delete row and a column using index and add new column and row to the existed data frame.

>>can someone please share me answer?

Hey Amarjeet,

I would need some more context to answer this question. Furthermore, please share the code you have tried yourself. I don’t want to do your entire assignment for you ðŸ˜‰

Regards,

Joachim

Hi, I have a question regarding the sequence.

I have got this vector :- v1 <- c(150, 150, 300, 300, 300). Now I want to have another vector with a same length based on v1 as follows

v2 <- c(v1[1], v1[2]+v1[1], v1[3]+v1[2]+v1[1], v1[4] +v1[3]+v1[2]+v1[1], v1[5]+v1[4]+ v1[3]+v1[2]+v1[1], length.out = length(v1))

How do write a script for this? sometimes I may not know the exact length so the script has to do it automatically based on length.

Hey Suman,

Are you looking for the “cumulative sum” of your vector? Have a look here for more details.

Regards,

Joachim

helow sir,

i want to ask that hoe to set y axis limit on a graph for unknow probability distribution?/

Hello Sana,

This is not to my knowledge. But we run a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well: https://www.facebook.com/groups/statisticsglobe

Regards,

Cansu

hi,

can you help me to get this

Write the R code to create the sequence starting from 8, with increments of size 1, ending at the unknown variable value z.

Hello,

Is the reason you want to use an unknown variable to use different values for the ending? Then you can define a function having a parameter for the ending variable. Please let me know if the solution addresses your question.

Regards,

Cansu

Create a sequence of 6 numbers varying from -50 to 50.

What happens when -Inf and Inf are chosen as starting and ending values, respectively?

please help me solve the question above.

Thanks

Hello Fatima,

R would return an error in such a case since you are trying to create an finite sequence with infinite end points defined.

Best,

Cansu