| vector {base} | R Documentation |
Vectors - Creation, Coercion, etc
Description
Avector inR is either an atomic vector i.e., one of the atomictypes, see ‘Details’, or of type (typeof) or modelist orexpression.
vector produces a ‘simple’ vector of the given length andmode, where a ‘simple’ vector has no attribute, i.e., fulfillsis.null(attributes(.)).
as.vector, a generic, attempts to coerce its argument into avector of modemode (the default is to coerce to whichevervector mode is most convenient): if the result is atomic(is.atomic), all attributes are removed.Formode="any", see ‘Details’.
is.vector(x) returnsTRUE ifx is a vector of thespecified mode having no attributesother than names.Formode="any", see ‘Details’.
Usage
vector(mode = "logical", length = 0)as.vector(x, mode = "any")is.vector(x, mode = "any")Arguments
mode | character string naming an atomic mode or |
length | a non-negative integer specifying the desired length. Foralong vector, i.e., |
x | anR object. |
Details
The atomic modes are"logical","integer","numeric" (synonym"double"),"complex","character" and"raw".
Ifmode = "any",is.vector may returnTRUE forthe atomic modes,list andexpression.For anymode, it will returnFALSE ifx has anyattributes except names. (This is incompatible with S.) On the otherhand,as.vector removesall attributes including namesfor results of atomic mode.
Formode = "any", and atomic vectorsx,as.vector(x)strips allattributes (includingnames),returning a simple atomic vector.
However, whenx is of type"list" or"expression",as.vector(x) currently returns theargumentx unchanged, unless there is anas.vector methodforclass(x).
Note that factors arenot vectors;is.vector returnsFALSE andas.vector converts a factor to a charactervector formode = "any".
Value
Forvector, a vector of the given length and mode. Logicalvector elements are initialized toFALSE, numeric vectorelements to0, character vector elements to"", rawvector elements tonul bytes and list/expression elements toNULL.
Foras.vector, a vector (atomic or of type list or expression).All attributes are removed from the result if it is of an atomic mode,but not in general for a list or expression result. The default method handles 24input types and 12 values oftype: the details of mostcoercions are undocumented and subject to change.
Foris.vector,TRUE orFALSE.is.vector(x, mode = "numeric") can be true for vectors of types"integer" or"double" whereasis.vector(x, mode = "double") can only be true for those of type"double".
Methods foras.vector()
Writers of methods foras.vector need to take care tofollow the conventions of the default method. In particular
Argument
modecan be"any", any of the atomicmodes,"list","expression","symbol","pairlist"or one of the aliases"double"and"name".The return value should be of the appropriate mode. For
mode = "any"this means an atomic vector or list or expression.Attributes should be treated appropriately: in particular whenthe result is an atomic vector there should be no attributes, noteven names.
is.vector(as.vector(x, m), m)should be true for anymodem, including the default"any".Currently this is not fulfilled inR when
m == "any"andxis of typelistorexpressionwithattributes in addition tonames— typically the case for(S3 or S4) objects (seeis.object) which are listsinternally.
Note
as.vector andis.vector are quite distinct from themeaning of the formal class"vector" in themethodspackage, and henceas(x, "vector") andis(x, "vector").
Note thatas.vector(x) is not necessarily a null operation ifis.vector(x) is true: any names will be removed from an atomicvector.
Non-vectormodes"symbol" (synonym"name") and"pairlist" are accepted but have long been undocumented: theyare used to implementas.name andas.pairlist, and those functions should preferably beused directly. None of the description here applies to thosemodes: see the help for the preferred forms.
References
Becker RA, Chambers JM, Wilks AR (1988).The New S Language.Chapman and Hall/CRC, London.
See Also
c,is.numeric,is.list, etc.
Examples
df <- data.frame(x = 1:3, y = 5:7)## Error:try(as.vector(data.frame(x = 1:3, y = 5:7), mode = "numeric"))x <- c(a = 1, b = 2)is.vector(x)as.vector(x)all.equal(x, as.vector(x)) ## FALSE###-- All the following are TRUE:is.list(df)! is.vector(df)! is.vector(df, mode = "list")is.vector(list(), mode = "list")