vector produces a vector of the given length and mode.
as.vector, a generic, attempts to coerce its argument into a
vector of mode
mode (the default is to coerce to whichever
vector mode is most convenient): if the result is atomic all
attributes are removed.
x is a vector of the
specified mode having no attributes other than names. It returns
vector(mode = "logical", length = 0) as.vector(x, mode = "any") is.vector(x, mode = "any")
- character string naming an atomic mode or
"expression"or (except for
- a non-negative integer specifying the desired length.
For a long vector, i.e.,
length > .Machine$integer.max, it has to be of type
"double". Supplying an argument of length other than one is an error.
- an R object.
The atomic modes are
mode = "any",
is.vector may return
the atomic modes,
mode, it will return
x has any
attributes except names. (This is incompatible with S.) On the other
as.vector removes all attributes including names
for results of atomic mode (but not those of mode
Note that factors are not vectors;
as.vector converts a factor to a character
mode = "any".
vector, a vector of the given length and mode. Logical vector elements are initialized to
FALSE, numeric vector elements to
0, character vector elements to
"", raw vector elements to
nulbytes and list/expression elements to
as.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 24 input types and 12 values of
type: the details of most coercions are undocumented and subject to change.For
is.vector(x, mode = "numeric")can be true for vectors of types
is.vector(x, mode = "double")can only be true for those of type
as.vector(x) is not necessarily a null operation if
is.vector(x) is true: any names will be removed from an atomic
"pairlist" are accepted but have long been undocumented: they
are used to implement
as.pairlist, and those functions should preferably be
used directly. None of the description here applies to those
modes: see the help for the preferred forms.
Writers of methods for
as.vector need to take care to
follow the conventions of the default method. In particular
"any", any of the atomic modes,
"pairlist"or one of the aliases
- The return value should be of the appropriate mode. For
mode = "any"this means an atomic vector or list.
- Attributes should be treated appropriately: in particular when the result is an atomic vector there should be no attributes, not even names.
is.vector(as.vector(x, m), m)should be true for any mode
m, including the default
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
df <- data.frame(x = 1:3, y = 5:7) ## Not run: ## Error: # as.vector(data.frame(x = 1:3, y = 5:7), mode = "numeric") # ## End(Not run) 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")
Example file for [LinkedIn Learning: R for Data Science: Lunchbreak Lessons](https://linkedin-learning.pxf.io/rwkly_vector). ```r # vectors are the simplest data storage in R # They are like standard variables I.am.a.vector <- 1 # except they are closer to arrays I.am.a.vector <- c(1,2,3) I.am.a.vector <- c("twas","brillig","and","the","slithey","toves") another.vector <- c(1,"twas") #concatenate different types into a vector results in character # number of members vs number of characters length(I.am.a.vector) == 6 # length is number of members, not length of string sum(nchar(I.am.a.vector)) == 29 # length of complete "string" # Vectors are not strings. I.am.also.a.vector <- c("did","gyre","and","gimble","in","the","wabes") paste(I.am.a.vector,I.am.also.a.vector) # maybe not what you expected c(I.am.a.vector,I.am.also.a.vector) # end for end combination. # indexing into a vector I.am.a.vector # first member is at position 1 I.am.a.vector # nope I.am.a.vector[-3] # select all but element three I.am.a.vector[1:3] # select elements 1, 2 and 3 # need something like a python dictionary? names(I.am.a.vector) <- c("first","second","third","fourth","fifth","sixth") I.am.a.vector["second"] ```