2
$\begingroup$

I am trying to retrieve the column names of the data set model$data using the following formula:

sample(colnames(model$data),1)

When I run it I receive the following error message:

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

Appreciate any help!

str(model) looks like this:

> 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"
$\endgroup$
2
  • $\begingroup$ Edit your question to show some summary information of model or better still a complete reproducible example showing us how to create a basic model object that exhibits this behaviour. $\endgroup$
    – Spacedman
    Mar 20, 2017 at 15:29
  • $\begingroup$ Hi, I already included the str for model $\endgroup$ Mar 20, 2017 at 15:59

2 Answers 2

6
$\begingroup$

Your data boils down to something like this structure:

> 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

but you have some other components that we don't need to bother with, and my data doesn't have row names and is a lot smaller.

model is a list of 2 (for me) and 13 (for you) parts.

The $data component is also a "List of 1" component.

So colnames(model$data) is trying to get the colnames of a list, and failing:

> colnames(model$data)
NULL

Which you would have spotted if you'd tried running colnames(model$data) yourself.

You want the colnames of the first element of the list model$data:

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

and hence:

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

Its possible that because this is a "kohonen" class object that there are functions that get these data matrices for you. You'll need to read the documentation to figure this out. What I've shown above is digging in the structure to find the data you want.

$\endgroup$
1
  • $\begingroup$ Thanks a lot for this! I'll check it the documentation of the package. $\endgroup$ Mar 20, 2017 at 18:01
0
$\begingroup$

$ is not valid as part of the data set (or frame) name since R uses it to denote column name (:)) so R actually tried to get the column name 'data' from the data frame named 'model' Try:

names(model)

And rename model$data to model

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"
$\endgroup$
4
  • $\begingroup$ Hi! the data is stored in different Matrices in the object model$data (or model[[1]] ). I am trying to retrieve the header of each matrix. $\endgroup$ Mar 19, 2017 at 21:26
  • $\begingroup$ Maybe you need a @ .... Can you paste the result of str(model) ?? $\endgroup$
    – Zahiro Mor
    Mar 19, 2017 at 21:29
  • $\begingroup$ the object model is the result of the formula som from the package Kohonen to create Self Organizing Maps. str(model) is copied above. $\endgroup$ Mar 19, 2017 at 21:33
  • $\begingroup$ str(model) gets a reply? $\endgroup$
    – Zahiro Mor
    Mar 19, 2017 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.