SoFunction
Updated on 2025-03-01

Calling method of seq function in R language

I saw that many readers browsed this article and were very happy. In order to help everyone better, I decided to modify it to help everyone better understand.

---------Modified on: April 28, 2018

In order to facilitate everyone to directly experiment and test the code in the development environment, below, I will give all the instructions and functions usage in English (avoid garbled code) and comment on it, hoping that it will be helpful to everyone!

First, let’s take a look at an example of the seq() function application!

x <- seq(0, 10, by = 0.01)
y <- sin(x)
plot(y)

Next, let’s look at the main methods of using functions!

Note: When calling functions in this article, the method of writing the entry and exit parameter names is used, such as:

seq(from = 1, to = 2)

This way, the call of the function is clearer, and when more functions are called, there will be no confusion or parameter matching errors.

Method 1: seq(from, to)

from: the starting point of the generated vector, to: the end point of the generated vector, the default step size is 1 (can be modified)

a <- seq(from = 1, to = 2)
# [1, 2]

Method 2: seq(from, to, by = )

by: Steps between vector elements

a <- seq(from = 1, to = 3, by = 0.5)
# [1, 1.5, 2, 2.5, 3]

Method 3: seq(from, to, = )

: Number of elements in a vector

a <- seq(from = 1, to = 3,  = 5)
# [1, 1.5, 2, 2.5, 3]

Method 4: seq( = )

: Indicates that the generated vector is an index of an existing vector element

x <- c(1.2, 5.2, 6.3, 4.6)
a <- seq( = x)
# [1, 2, 3, 4]

Method 5: seq(from)

The four functions of this method are similar

x <- c(1.2, 5.2, 6.3, 4.6)
a <- seq(from = x)
# [1, 2, 3, 4]

Method 6: seq( = )

Generate vectors with a step size of 1 and a length of 1

a <- seq( = 5)
# [1, 2, 3, 4, 5]

The above methods are more common methods, and the detailed function description is as follows:

Sequence Generation

Description

Generate regular sequences. seq is a standard generic with a default method. is a primitive which can be much faster but has a few restrictions. seq_along and seq_len are very fast primitives for two common cases.

---------------------------------------------

Usage

seq(...)
## Default S3 method:
seq(from = 1, to = 1, by = ((to - from)/( - 1)),
= NULL, = NULL, ...)
(from, to, by, , , ...)
seq_along()

seq_len()

---------------------------------------------

Arguments

1:...

arguments passed to or from methods.

2:from, to

the starting and (maximal) end values of the sequence. Of length 1 unless just from is supplied as an unnamed argument.

3:by

number: increment of the sequence.

4:

desired length of the sequence. A non-negative number, which for seq and will be rounded up if fractional.

5:

take the length from the length of this argument.

Reference: /jiluben/article/details/40024607

This is the article about the call method of the R language seq() function. For more related content of the R language seq() function, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!