How do I manipulate/access elements of an instance of “dist” class using core R?

前端 未结 12 1970
傲寒
傲寒 2021-02-02 10:50

A basic/common class in R is called \"dist\", and is a relatively efficient representation of a symmetric distance matrix. Unlike a \"matrix\" object,

12条回答
  •  长情又很酷
    2021-02-02 11:14

    You can acces the atributes of any object with str()

    for a "dist" object of some of my data (dist1), it look like this:

    > str(dist1)
    Class 'dist'  atomic [1:4560] 7.3 7.43 7.97 7.74 7.55 ...
      ..- attr(*, "Size")= int 96
      ..- attr(*, "Labels")= chr [1:96] "1" "2" "3" "4" ...
      ..- attr(*, "Diag")= logi FALSE
      ..- attr(*, "Upper")= logi FALSE
      ..- attr(*, "method")= chr "euclidean"
      ..- attr(*, "call")= language dist(x = dist1) 
    

    you can see that for this particular data set, the "Labels" attribute is a character string of length = 96 with numbers from 1 to 96 as characters.

    you could change directly that character string doing:

    > attr(dist1,"Labels") <- your.labels
    

    "your.labels" should be some id. or factor vector, presumably in the original data from with the "dist" object was made.

提交回复
热议问题