在 R 中检索列名

数据挖掘 r
2022-02-26 06:58:46

我正在尝试使用以下公式检索数据集 model$data 的列名:

sample(colnames(model$data),1)

当我运行它时,我收到以下错误消息:

Error in sample.int(length(x), size, replace, prob) : 
  invalid first argument

感谢任何帮助!

str(model) 看起来像这样:

> str(model)
    List of 13
     $ data            :List of 1
      ..$ : num [1:1000, 1:56] 1 1 1 1 0 1 1 0 1 1 ...
      .. ..- attr(*, "dimnames")=List of 2
      .. .. ..$ : chr [1:1000] "7530" "5975" "552" "815" ...
      .. .. ..$ : chr [1:56] "Agriculture_and_Hunting" "Baking" "Biochemistry" "Braiding" ...
     $ unit.classif    : num [1:1000] 3 5 5 5 16 3 5 1 3 3 ...
     $ distances       : num [1:1000] 0.000806 0.000239 0.000239 0.000239 0.001953 ...
     $ grid            :List of 6
      ..$ pts              : num [1:25, 1:2] 1.5 2.5 3.5 4.5 5.5 1 2 3 4 5 ...
      .. ..- attr(*, "dimnames")=List of 2
      .. .. ..$ : NULL
      .. .. ..$ : chr [1:2] "x" "y"
      ..$ xdim             : num 5
      ..$ ydim             : num 5
      ..$ topo             : chr "hexagonal"
      ..$ neighbourhood.fct: Factor w/ 2 levels "bubble","gaussian": 1
      ..$ toroidal         : logi FALSE
      ..- attr(*, "class")= chr "somgrid"
     $ codes           :List of 1
      ..$ : num [1:25, 1:56] 0.000388 0.99996 1 1 1 ...
      .. ..- attr(*, "dimnames")=List of 2
      .. .. ..$ : chr [1:25] "V1" "V2" "V3" "V4" ...
      .. .. ..$ : chr [1:56] "Agriculture_and_Hunting" "Baking" "Biochemistry" "Braiding" ...
     $ changes         : num [1:100, 1] 0.00261 0.00263 0.00262 0.00254 0.00254 ...
     $ alpha           : num [1:2] 0.05 0.01
     $ radius          : Named num [1:2] 3 0
      ..- attr(*, "names")= chr [1:2] "67%" ""
     $ user.weights    : num 1
     $ distance.weights: num 1
     $ whatmap         : int 1
     $ maxNA.fraction  : int 0
     $ dist.fcts       : chr "sumofsquares"
     - attr(*, "class")= chr "kohonen"
2个回答

您的数据归结为以下结构:

> str(model)
List of 2
 $ data:List of 1
  ..$ : int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:4] "a" "b" "c" "d"
 $ foo : num 1

但是您还有一些我们不需要打扰的其他组件,而且我的数据没有行名并且要小得多。

model是 2 个(对我而言)和 13 个(对你而言)零件的列表。

$data组件也是“List of 1”组件。

colnames(model$data)尝试获取列表也是如此colnames,但失败了:

> colnames(model$data)
NULL

如果您尝试colnames(model$data)自己跑步,您会发现这一点。

您想要colnames列表的第一个元素model$data

> colnames(model$data[[1]])
[1] "a" "b" "c" "d"

因此:

> sample(colnames(model$data[[1]]),1)
[1] "b"

可能因为这是一个“kohonen”类对象,所以有一些函数可以为您获取这些数据矩阵。您需要阅读文档才能弄清楚这一点。我上面展示的是在结构中挖掘以找到您想要的数据。

$ 作为数据集(或框架)名称的一部分是无效的,因为 R 使用它来表示列名 (:))所以 R 实际上试图从名为“模型”的数据框中获取列名“数据”尝试:

names(model)

并将 model$data 重命名为 model

str(模型)

> str(model)
List of 13
 $ data            :List of 1
  ..$ : num [1:1000, 1:56] 1 1 1 1 0 1 1 0 1 1 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:1000] "7530" "5975" "552" "815" ...
  .. .. ..$ : chr [1:56] "Agriculture_and_Hunting" "Baking" "Biochemistry" "Braiding" ...
 $ unit.classif    : num [1:1000] 3 5 5 5 16 3 5 1 3 3 ...
 $ distances       : num [1:1000] 0.000806 0.000239 0.000239 0.000239 0.001953 ...
 $ grid            :List of 6
  ..$ pts              : num [1:25, 1:2] 1.5 2.5 3.5 4.5 5.5 1 2 3 4 5 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..$ xdim             : num 5
  ..$ ydim             : num 5
  ..$ topo             : chr "hexagonal"
  ..$ neighbourhood.fct: Factor w/ 2 levels "bubble","gaussian": 1
  ..$ toroidal         : logi FALSE
  ..- attr(*, "class")= chr "somgrid"
 $ codes           :List of 1
  ..$ : num [1:25, 1:56] 0.000388 0.99996 1 1 1 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:25] "V1" "V2" "V3" "V4" ...
  .. .. ..$ : chr [1:56] "Agriculture_and_Hunting" "Baking" "Biochemistry" "Braiding" ...
 $ changes         : num [1:100, 1] 0.00261 0.00263 0.00262 0.00254 0.00254 ...
 $ alpha           : num [1:2] 0.05 0.01
 $ radius          : Named num [1:2] 3 0
  ..- attr(*, "names")= chr [1:2] "67%" ""
 $ user.weights    : num 1
 $ distance.weights: num 1
 $ whatmap         : int 1
 $ maxNA.fraction  : int 0
 $ dist.fcts       : chr "sumofsquares"
 - attr(*, "class")= chr "kohonen"