The R Project SVN R

Rev

Rev 77633 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/sequence.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
89122 maechler 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{sequence}
7
\title{Create A Vector of Sequences}
8
\usage{
77633 lawrence 9
sequence(nvec, \dots)
89122 maechler 10
\method{sequence}{default}(nvec, from = 1L, by = 1L,
11
    recycle = Sys.getenv("R_sequence_recycle", "false"), \dots)
2 r 12
}
56186 murdoch 13
\alias{sequence}
77633 lawrence 14
\alias{sequence.default}
2 r 15
\arguments{
77633 lawrence 16
  \item{nvec}{coerced to a non-negative integer vector each element of which
17
    specifies the length of a sequence.}
18
  \item{from}{coerced to an integer vector each element of which
19
    specifies the first element of a sequence.}
20
  \item{by}{coerced to an integer vector each element of which
21
    specifies the step size between elements of a sequence.}
89122 maechler 22
  \item{recycle}{\code{\link{logical}} or coercible to it, indicating if
23
    \code{nvec} is recycled, as it has always been documented to.
24
    We recommend to set this to \code{TRUE} explicitly, as that will become
25
    default, see \sQuote{Details}.}% also \PR{18304} for background
77633 lawrence 26
  \item{\dots}{additional arguments passed to methods.}
2 r 27
}
28
\description{
77633 lawrence 29
  The default method for \code{sequence} generates the sequence
30
  \code{\link{seq}(from[i], by = by[i], length.out = nvec[i])} for each
31
  element \code{i} in the parallel (and recycled) vectors \code{from},
89122 maechler 32
  \code{by} and \code{nvec}.
33
  Note that \code{nvec} is \emph{not} recycled unless \code{recycle}
34
  is true, see \sQuote{Details}.
35
  It then returns the result of concatenating those sequences.
2 r 36
}
42342 ripley 37
\details{
77633 lawrence 38
  Negative values are supported for \code{from} and
39
  \code{by}. \code{sequence(nvec, from, by=0L)} is equivalent to
40
  \code{rep(from, each=nvec)}.
42515 maechler 41
 
89122 maechler 42
  This function was originally implemented in \R with fewer features, but
77633 lawrence 43
  it has since become more flexible, and the default method is
44
  implemented in C for speed.
89122 maechler 45
 
46
  Argument \code{recycle} is new since \R 4.6.0;
47
  currently, the default is \code{FALSE} unless the environment variable
48
  \env{R_sequence_recycle} is set to a true value.  \cr
49
  This provides back compatibility with \R <= 4.5.z, where \code{from} and
50
  \code{by} are recycled or shortened to length \code{length(nvec)} in case
51
  that is shorter than the maximal length.  \cr
52
  The plan is to replace the environment variable with an option
53
  (\code{\link{getOption}}) defaulting to \code{TRUE} and later to
54
  \code{TRUE} without a global option, to use \R's usual recycling semantic
55
  for all three arguments \code{nvec, from, by}.
42342 ripley 56
}
2 r 57
\seealso{
42342 ripley 58
  \code{\link{gl}}, \code{\link{seq}}, \code{\link{rep}}.
2 r 59
}
77633 lawrence 60
\author{Of the current version, Michael Lawrence based on code from the
89122 maechler 61
  \pkg{S4Vectors} Bioconductor package}
2 r 62
\examples{
61168 ripley 63
sequence(c(3, 2)) # the concatenated sequences 1:3 and 1:2.
89122 maechler 64
#> [1] 1 2 3 ' 1 2  (using ' to visualize the sub-sequences)
65
sequence(c(3, 2), from=2L)         #> [1] 2 3 4 ' 2 3
66
sequence(c(3, 2), from=2L, by=2L)  #> [1] 2 4 6 ' 2 4
67
sequence(c(3, 2), by=c(-1L, 1L))   #> [1] 1 0 -1 ' 1 2
68
 
69
## Cases where  'recycle'  makes a difference:
70
sequence(3, 1:3) #>  1 2 3  (_currently_ -- will change in future!)
71
sequence(3, 1:3, recycle = FALSE)# back compatible: 1 2 3
72
sequence(3, 1:3, recycle = TRUE) # future default -- use in new code!
73
                                    # --> 1 2 3 ' 2 3 4 ' 3 4 5
74
try(sequence(3, 1:3, 1[0], recycle = FALSE)) # an Error; default will be "empty":
75
    sequence(3, 1:3, 1[0], recycle = TRUE) # |--> integer(0)
2 r 76
}
286 maechler 77
\keyword{manip}