vector(mode="logical", length=0) is.vector(x, mode="any") as.vector(x, mode="any")
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 will match, that is,
an object with no attributes.
Note that a list is typically a vector in this sense.
as.vector returns an object of the same length as
x and 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",
the return value has storage mode
"double".
If the specified mode is
"any", the effect is
to create an object something like x with no attributes
(except allowing names if x is a recursive object, which
is an implementation detail).
By default S-PLUS just removes attributes, but for factors it first
converts to a character vector (because factors are more
like character data than integer data).
bdVector to a
vector, you must call
bd.coerce.
There is a difference between coercing to a simple object
and setting the
mode attribute:
mode(x) <- mode(y)
This changes the mode of
x but leaves all other attributes unchanged
(so a matrix stays a matrix). The value of
as.vector(x,mode(y))
would have no attributes. Similarly,
is.vector would return
FALSE for
a matrix.
Note that the idea of a vector here differs from what you might think
of as a vector -- an object without dimensions (or only one dimension)
which is not hierarchical (not a
list).
See examples below on how to test for that.
Also note that a vector as used here differs from the
class
"vector";
is.vector(x)
and
is(x,"vector")
are different:
the latter tests whether
x
inherits from the new-style
"vector"
class.
A list may have names and still test
TRUE with
is.vector, but
atomic objects with names will test
FALSE.
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) # F, because x has a names attribute
length(dim(x)) == 0 && !is.list(x) # T, x is a vector in the ordinary sense
length(dim(x)) <= 1 && !is.list(x) # T, alternative form, allows 1-d array
y <- list(a=1, b=2)
is.vector(y) # T, because y has no attributes (names are allowed for a list)
length(dim(y)) == 0 && !is.list(y) # F, not a vector in the ordinary sense