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 the TRUE , 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 c; (1 , NA); y & lt; - na.omit (x); print (y) with the output to see how it can look). Here, y has an attribute, na.action , which is an object with an attribute, class . z and look at its properties with 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 code by typing NULL

; - 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") .

< P> It has all been written in the R manual (where I had seen it before), but I do not remember what section.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -