c
Combine Values into a Vector or List

Description

Concatenates objects into a vector or a list. The result is an atomic list if all of the objects are atomic; otherwise, the result is a list.

Usage

c(..., recursive = FALSE)
## S3 method for class 'Date':
c(..., recursive = FALSE)
## S3 method for class 'POSIXct':
c(..., recursive = FALSE)
## S3 method for class 'POSIXlt':
c(..., recursive = FALSE)
## S3 method for class 'factor':
c(..., recursive = TRUE)

Arguments

... any objects. Missing values (NAs) are allowed. The names of arguments become part of the names of the resulting vector.
recursive a logical value. If TRUE, indicates that the combining should be done recursively, going up into lists. The default is FALSE, but methods, such as c.factor, can override this.

Details

Arguments that are NULL or length 0 do not contribute elements to the result.
Note that c(...,recursive=TRUE) is equivalent to unlist(list(...), recursive=TRUE).
The default method for the c function also supports specifying a use.names argument, with default value TRUE. This argument is interpreted similar to the use.names of unlist. It is not displayed in the argument list above, because it is not present in the S3/S4 generics for 'c'.
c.factor was introduced in TERR-6.0. Prior to that, combining factors with c gave an integer vector that was the concatenation of the integer codes of the factor arguments.
Value
returns a vector or a list that is the combination of all values from all arguments to the function. If recursive is TRUE, arguments with recursive modes are effectively unlisted (see unlist) before they are used.
The mode of the result is the most general of all the modes in the arguments (or the unlisted arguments, if recursive=TRUE). In particular, list objects can be combined this way.
The names attribute of the result is generated from the argument names, if any, plus the names of the combined objects. See unlist for the rule used. Attributes of objects that are bound with other objects are deleted.
The c.Date(), c.POSIXct(), and c.POSIXlt() return combined date-time objects of their respective class type.
c.factor(), if all of its arguments are factors, returns a factor whose levels attribute contains the union of the levels attributes of its arguments. If all the arguments are ordered factors with identical levels, then the outut is an ordered factor with those levels.
See Also
concat, append, cbind, unlist
Examples
c(1:10, 1:5, 1:10)
c(2, 3, 5, 7, 11, 13)
c(a=1, b=2)  # vector with names "a" and "b"
c(Sdatasets::state.name, "Washington DC")
# a list of 4 numeric vectors
c(list(1:3, a=3:6), list(8:23, c(3, 8, 39)))
# a numeric vector of length 26
c(list(1:3, a=3:6), list(8:23, c(3, 8, 39)), recursive=TRUE)
# build x, element by element
# useful if final length not known in advance
## Not run: 
x <- numeric(0)
for(i in possibles)
  if(test(i)) x <- c(x, fun(i))

## End(Not run)

## combine date-time objects c(Sys.Date() + 365, Sys.Date() + 2, as.Date("2009-12-12")) c(Sys.time() + 365, Sys.time() + 2, as.POSIXct("2009-12-12 01:02:03")) c(as.POSIXlt(Sys.time() + 365), as.POSIXlt(Sys.time() + 2), as.POSIXlt("2009-12-12 01:02:03"))

## combine factors c(factor("A", levels=c("C","B","A")), factor(c("C","D")))

## demonstrate the use.names argument c(c(A=1,B=2),c(Z=3)) ## returns c(A=1,B=2,Z=3) c(c(A=1,B=2),c(Z=3),use.names=FALSE) ## returns c(1,2,3)

Package base version 6.1.2-7
Package Index