Windstorm1981 Windstorm1981 - 2 months ago 19
R Question

Saving Agnes Object in R

Apologies if this is really basic - is there a way to save an Agnes object from the Cluster library in R to a file for use in another session? I've looked at the CRAN pdf and for a similar question here on Stackoverflow but don't see anything.

Thanks in advance.

Answer

As mentioned in the comments:

library(cluster)

data(animals)
aa.ga <- agnes(animals, method = "gaverage")

saveRDS(aa.ga,"aa.ga.RDS") # Save it

rm(aa.ga) # Remove it so I can prove this works

aa.ga <- readRDS("aa.ga.RDS") # Read it back in

aa.ga
Call:  agnes(x = animals, method = "gaverage")  Agglomerative coefficient:  0.8285911  Order of objects:  

[1] ant lob fly her liz sal bee cpl spi cat chi man cow lio rab duc eag ele wha fro Height (summary): Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0000 0.0000 1.2050 0.9573 1.5890 2.6230

Available components: [1] "order"     "height"    "ac"        "merge"  "diss"      "call"      "method"    "order.lab" "data"