r - When does is.vector() return true? -
I am relatively new to R. When reading documents, the following sentence confuses me
is.vector () does not test that any object is a vector. Instead it only gives TRUE if there is no attribute other than the object name.
TRUE TRUE back to is.vector () list can be a good reference < / P>
When is is.vector ()
correct?
is.vector ()
does not test that any The object is a vector, instead it gives theTRUE
, only if the object is not different from the name with a feature.
The quote means what it says. Why says that what he says is more subtle.
All R objects are linked to a list of "attributes", which attributes ()
for the entire list) attr ()
(individual attributes with for. In fact, the square of an object is actually a special feature that generic (S3) functions use to determine which method to use.
This property is "silent" in most cases, but it is visible na.omit ()
function (try x
to see how it can look). Here, y
has an attribute, na.action
, which is an object with an attribute, class
. z
attributes (z)
and then atr (z, "Class")
.
When there is a class
attribute, R uses the R to overwrite the square of the underlying object; In this case, the do not leave
class overrides the internal integer
class. Try etter (z, "square") & lt; - NULL; Square (z)
. Note that this is done directly under the class (z) & lt; - Specify z directly with
; - attr (y, "na .action") Try again is.vector (z)
. Then remove your class from NULL
and then try again is.vector (z)
. It is expected that it should be clarified that the check for is.vector
is the fact that a particular use of the class
attribute in method transmission is irrelevant. In fact, you can do the same thing for y
: try is.vector (y)
, is.vector (x [! Is.na (X)]
.
The reason for this criterion is that many objects in R, which do not look or function like vectors, are stored internally as vectors. Matrix is in this category. Try something like x & lt; - Matrix (1: 4, 2); dput (x)
. You will see that x
internally As vector 1: 4
.dim
with special code. This feature is hidden and inaccessible; try attr (x, ".dim")
.
Comments
Post a Comment