Why is the class of a vector the class of the elements of the vector and not vector itself?

前端 未结 4 1880
时光说笑
时光说笑 2020-12-16 02:48

I don\'t understand why the class of a vector is the class of the elements of the vector and not vector itself.

vector <- c(\"la\", \"la\", \"la\")
class         


        
4条回答
  •  时光取名叫无心
    2020-12-16 03:33

    Here's the best diagram I've found that lays out the class hierarchy used by the class function:

    Although the class names don't correspond exactly with the results of the R class function, I believe the hierarchy is basically accurate. The key to your answer is that the class function only gives the root class in the hierarchy.

    You will see that Vector is not a root class. The root class for your example would be StrVector, which corresponds to the "character" class, the class for a vector with character elements. In contrast, Matrix is itself a root class; hence, its class is "matrix".

提交回复
热议问题