| vector | R Documentation |
vector produces a vector of the given length and mode.
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 allattributes are removed.
is.vector returnsTRUE ifx is a vector of thespecified mode having no attributesother than names. It returnsFALSE otherwise.
vector(mode = "logical", length = 0)as.vector(x, mode = "any")is.vector(x, mode = "any")
mode | character string naming an atomic mode or |
length | a non-negative integer specifying the desired length. Fora long vector, i.e., |
x | anR object. |
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 (but not those of mode"list" nor"expression").
Note that factors arenot vectors;is.vector returnsFALSE andas.vector converts a factor to a charactervector formode = "any".
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 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".
as.vector()Writers of methods foras.vector need to take care tofollow the conventions of the default method. In particular
Argumentmode can 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. Formode = "any" this means an atomic vector or list.
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".
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.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
c,is.numeric,is.list, etc.
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")
Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.