vector
Vectors (Simple Objects)

Description

Creates or tests for vectors.

Usage

vector(mode = "logical", length = 0L) 
is.vector(x, mode = "any") 
as.vector(x, mode = "any")

Arguments

x any object.
mode a character string giving the wanted mode.
length an integer value giving the wanted length.

Details

Note the difference between coercing to a simple object and setting the mode attribute: Similarly, is.vector returns FALSE for a matrix.
Note that a vector is an object without dimensions (or only one dimension), which is not hierarchical (not a list). See the Examples section on how to test for such an object without dimensions (or only one dimension), not a list.
Also note that a vector as used here differs from the class "vector". The functions is.vector(x) and is(x,"vector") are different: the latter tests whether x inherits from the new-style "vector" class.
Value
vector returns a simple object of the desired mode and length.
is.vector returns TRUE if x is a simple object of the mode specified. Otherwise, it returns FALSE. If the mode is "any", any simple object matches; that is, it can be an object with no attributes. Note that, in this sense, a list is typically a vector.
as.vector returns an object of the same length as x, with data resulting from coercing the elements of x to the specified mode. For most simple objects, the return value is simply x.
  • If the specified mode is "numeric", this function returns an object with the storage mode "double".
  • If the specified mode is "any" (the default), this function creates an object like x, but with no attributes. By default, running as.vector with mode "any" just removes attributes; however, if x is a factor (which is more like character data than integer data), first it is converted to a character vector.
Bugs
A list may have names and still test TRUE with is.vector, but atomic objects with names will test FALSE.
See Also
c, list, mode, attributes, is.recursive.
Examples
vector("list", 10) # list of length 10, initialized to null elements. 
as.vector(x, mode(y)) # make a simple object of same mode as y 
is.vector(list(1:3)) # returns TRUE 
is.vector(matrix(NA,2,3)) # returns FALSE 

x <- 1:5 names(x) <- letters[1:5] is.vector(x) # TRUE

# TRUE, x is a vector in the ordinary sense length(dim(x)) == 0 && !is.list(x)

# TRUE, alternative form, allows 1-d array length(dim(x)) <= 1 && !is.list(x)

y <- list(a=1, b=2)

# T, because y has no attributes (names are allowed for a list) is.vector(y)

# F, not a vector in the ordinary sense length(dim(y)) == 0 && !is.list(y)

Package base version 6.1.1-7
Package Index