why the object is vector?

前端 未结 3 836
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 17:56
> x=c(1,2,3,4,5)  
> x1=list(n1=1,n2=2,n3=x)  
> is.vector(x1)  

[1] TRUE

> is.list(x1)  

[1] TRUE

相关标签:
3条回答
  • 2020-12-21 18:19

    From ?is.vector

    If mode = "any", is.vector may return TRUE for the atomic modes, list and expression.

    You can specify the mode if you do not want is.vector to return TRUE for a list

    > is.vector(x1, mode='numeric')
    [1] FALSE
    
    > is.vector(x, mode='numeric')
    [1] TRUE
    
    0 讨论(0)
  • 2020-12-21 18:38

    A vector in R is an ordered collection of stuff. Stuff in this case is

    > mode(x1)
    [1] "list"
    

    from the help file

    is.vector returns TRUE if x is a vector of the specified mode having no attributes other than names.

    > attributes(x1)
    $names
    [1] "n1" "n2" "n3"
    

    if we were to give x1 another attribute:

    levels(x1)<-1:3
    
    > x1
    $n1
    [1] 1
    
    $n2
    [1] 2
    
    $n3
    [1] 1 2 3 4 5
    
    attr(,"levels")
    [1] 1 2 3
    
    > is.list(x1)
    [1] TRUE
    
    > is.vector(x1)
    [1] FALSE
    

    it would still be a list but not now a vector

    From A brief history of S "The basic data structure in S is a vector of like­elements: numbers, character strings, or logical val­ ues. Although the notion of an attribute for an S object wasn't clearly implemented until the 1988 release, from the beginning S recognized that the primary vector of data was often accompanied by other values that described special properties of the data. For example, a matrix is just a vector of data along with an auxil­ iary vector named Dim that tells the dimensionality (number of rows and columns). Similarly, a time series has a Tsp attribute to tell the start time, end time, and number of observations per cycle. These vectors with attributes are known as vector structures, and this distinguishes S from most other systems."

    Presumably it is similar in R which is an implementation of S so these vector structures are not designated as vectors.

    0 讨论(0)
  • 2020-12-21 18:40
    > is.atomic(x1)
    [1] FALSE
    

    From the R language definition, lists are generic vectors, but not atomic vectors.

    0 讨论(0)
提交回复
热议问题