【问题标题】:How to find the real datatype of a variable in R?如何在 R 中找到变量的真实数据类型?
【发布时间】:2016-06-25 21:04:42
【问题描述】:

R 中的数据类型一直让我感到困惑,如果这是一个基本问题,我很抱歉。我知道 R 中的 $loadings 属性应该是一个矩阵。甚至文档都说如果你输入?loadings:它说它是the matrix of variable loadings

arrests_pca <- princomp(USArrests, cor=TRUE)
typeof(arrests_pca$loadings)

....返回

[1] "double"

...所以我试图找到类:

> class(arrests_pca$loadings)
[1] "loadings"

【问题讨论】:

  • is.matrix(arrests_pca$loadings)。您还可以使用unclass() 删除加载类,它会变成“真实”矩阵。是 print 方法将其置于不同的类中,但它实际上是一个底层矩阵。
  • 所以,你必须知道它是一个矩阵才能尝试,对吧?
  • 没有。它会告诉你 TRUE 或 FALSE。但是你可以通过在控制台中查看它可以看出它可能是一个矩阵或数据框。此外,您始终可以使用默认的打印方法来查看其底层结构。 print.default(arrests_pca$loadings)
  • @Candic3 ...如果您阅读文档,这并不太难...
  • str(arrests_pca$loadings) 显示它是一个二维数字,尽管它没有明确表示“矩阵”。

标签: r matrix types pca


【解决方案1】:

使用str(arrests_pca$loadings)。它返回

 loadings [1:4, 1:4] -0.536 -0.583 -0.278 -0.543 0.418 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:4] "Murder" "Assault" "UrbanPop" "Rape"
  ..$ : chr [1:4] "Comp.1" "Comp.2" "Comp.3" "Comp.4"

您可以在第一行看到它是一个 4x4 矩阵。

【讨论】:

  • 只有你知道[1:4, 1:4]是什么意思
  • 也许编辑你的最后一行说“第一行显示结构是一个 4x4 矩阵” - 即不要暗示 OP 应该已经知道这一点/它应该是显而易见的?
【解决方案2】:

使用mode()

mode(arrests_pca$loadings)

【讨论】:

    猜你喜欢
    • 2016-06-18
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2017-07-17
    相关资源
    最近更新 更多