Combine Values into a Vector or List (original) (raw)
Description
This is a generic function which combines its arguments.
The default method combines its arguments to form a vector. All arguments are coerced to a common type which is the type of the returned value, and all attributes except names are removed.
Usage
## S3 Generic function
c(...)
## Default S3 method:
c(..., recursive = FALSE, use.names = TRUE)
Arguments
... | objects to be concatenated. All NULL entries are dropped before method dispatch unless at the very beginning of the argument list. |
---|---|
recursive | logical. If recursive = TRUE, the function recursively descends through lists (and pairlists) combining all their elements into a vector. |
use.names | logical indicating if names should be preserved. |
Details
The output type is determined from the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression. Pairlists are treated as lists, whereas non-vector components (such as [name](../../base/help/name.html)
s / symbol
s and [call](../../base/help/call.html)
s) are treated as one-element [list](../../base/help/list.html)
s which cannot be unlisted even if recursive = TRUE
.
If the output type is [complex](../../base/help/complex.html)
, logical, integer, and doubleNA
s keep their imaginary parts zero when coerced, and hence will_not_ become NA_complex_
(with imaginary part NA
).
There is a [c.factor](../../base/help/c.factor.html)
method which combines factors into a factor.
c
is sometimes used for its side effect of removing attributes except names, for example to turn an [array](../../base/help/array.html)
into a vector.as.vector
is a more intuitive way to do this, but also drops names. Note that c
methods other than the default are not required to remove attributes (and they will almost certainly preserve a class attribute).
This is a primitive function.
Value
NULL
or an expression or a vector of an appropriate mode. (With no arguments the value is NULL
.)
S4 methods
This function is S4 generic, but with argument list(x, ...)
.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language. Wadsworth & Brooks/Cole.
See Also
[unlist](../../base/help/unlist.html)
and [as.vector](../../base/help/as.vector.html)
to produce attribute-free vectors.
Examples
c(1, 7:9)
c(1:5, 10.5, "next")
## uses with a single argument to drop attributes
x <- 1:4
names(x) <- letters[1:4]
x
c(x) # has names
as.vector(x) # no names
dim(x) <- c(2,2)
x
c(x)
as.vector(x)
## append to a list:
ll <- list(A = 1, c = "C")
## do *not* use
c(ll, d = 1:3) # which is == c(ll, as.list(c(d = 1:3)))
## but rather
c(ll, d = list(1:3)) # c() combining two lists
## descend through lists:
c(list(A = c(B = 1)), recursive = TRUE)
c(list(A = c(B = 1, C = 2), B = c(E = 7)), recursive = TRUE)
[Package _base_ version 4.6.0 Index]