[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  c: Combine Values into a Vector or List

##  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(…)
    
    # S3 method for default
    c(…, recursive = FALSE, use.names = TRUE)

##  Arguments

…

objects to be concatenated.

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 ](/link/names?package=base&version=3.6.2) `
should be preserved.

##  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, ...) ` .

##  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 names and calls) are treated as one-element lists which cannot be
unlisted even if ` recursive = TRUE ` .

Note that ` [ factor ](/link/factor?package=base&version=3.6.2) ` s are
treated only via their internal ` [ integer
](/link/integer?package=base&version=3.6.2) ` codes; one proposal has been to
use

    
    
       c.factor <- function(..., recursive=TRUE) unlist(list(...), recursive=recursive)

if factor concatenation by ` c() ` should give a ` [ factor
](/link/factor?package=base&version=3.6.2) ` .

` c ` is sometimes used for its side effect of removing attributes except
names, for example to turn an array into a vector. ` as.vector ` is a more
intuitive way to do this, but also drops names. Note that methods other than
the default are not required to do this (and they will almost certainly
preserve a class attribute).

This is a [ primitive ](/link/primitive?package=base&version=3.6.2) function.

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

##  See Also

` [ unlist ](/link/unlist?package=base&version=3.6.2) ` and ` [ as.vector
](/link/as.vector?package=base&version=3.6.2) ` to produce attribute-free
vectors.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fc&utm_source=r-docs&utm_medium=docs&utm_term=c&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    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
    
    c(list(A = c(B = 1)), recursive = TRUE)
    
    c(options(), recursive = TRUE)
    c(list(A = c(B = 1, C = 2), B = c(E = 7)), recursive = TRUE)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fc&utm_source=r-docs&utm_medium=docs&utm_term=c&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)