【问题标题】:Retrieve best number of clusters from NbClust从 NbClust 中检索最佳集群数
【发布时间】:2016-05-18 07:27:26
【问题描述】:

R 中的许多函数都提供了某种控制台输出(例如 NbClust() 等)。有没有办法在不查看输出的情况下检索某些输出(例如某个整数值)?从控制台读取的任何方式?

想象一下,package manual 中提供的示例代码的输出如下所示:

[1] "Frey index : No clustering structure in this data set"
*** : The Hubert index is a graphical method of determining the number of clusters.
                In the plot of Hubert index, we seek a significant knee that corresponds to a 
                significant increase of the value of the measure i.e the significant peak in Hubert
                index second differences plot. 

*** : The D index is a graphical method of determining the number of clusters. 
                In the plot of D index, we seek a significant knee (the significant peak in Dindex
                second differences plot) that corresponds to a significant increase of the value of
                the measure. 

******************************************************************* 
* Among all indices:                                                
* 1 proposed 2 as the best number of clusters 
* 2 proposed 4 as the best number of clusters 
* 2 proposed 6 as the best number of clusters 
* 1 proposed 7 as the best number of clusters 

                   ***** Conclusion *****                            

* According to the majority rule, the best number of clusters is  4 


*******************************************************************

如何从上述输出的最后一行检索值 4

【问题讨论】:

  • 否,x$Best.nc 返回一个列表,其中包含每个索引各自的“簇数”值。但这不是我感兴趣的,我想取回最后一行的值……
  • @zx8754 如上所述,我真的在寻找一种从控制台输出中检索信息的方法,不仅适用于NbClust(),而且通常适用于 R 中的函数生成的任何输出
  • 是的,这行得通!谢谢@zx8754 谢谢你还指出使用控制台输出并不常见,你想把它作为这个问题的答案吗?

标签: r console console-output


【解决方案1】:

最好使用对象而不是在控制台中输出。任何 "good" 函数都会返回有希望的结构化输出,可以使用 $@ 符号访问,使用 str() 查看对象的结构。

在你的情况下,我认为这应该可行:

length(unique(res$Best.partition))

【讨论】:

    【解决方案2】:

    另一种选择是:

    max(unlist(res[4]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 2012-04-07
      • 1970-01-01
      • 2019-01-16
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多