Home > Software engineering >  R: Argument to table is a variable containing variable name, but is not interpreted as such?
R: Argument to table is a variable containing variable name, but is not interpreted as such?

Time:05-30

I am calling table in a loop, so the variable name have to be in a variable itself. Reproducible code below:

library(FactoMineR)
data(hobbies)

htable <- matrix(as.numeric(NA), nrow=18, ncol=7,
                 dimnames=list( colnames(hobbies)[1:18],
                                names(with(hobbies, 
table(Profession)))))

### Then filling the tables with values:

 for(hob in rownames(htable)) {
    htable[hob, ] <- with(hobbies, table(hob, 
Profession))[2, ]
}
    > Error in table(hob, Profession) : all arguments must 
have the same length

Somehow, the length of hob is taken as 1, so it is not interpreted in the context of the data frame hobbies? What is wrong?

CodePudding user response:

We can use [ instead of with

for(hob in rownames(htable))  
   htable[hob, ] <- table(hobbies[, hob], 
   hobbies[, "Profession"])[2,] 

-output

> htable
                Unskilled worker Manual labourer Technician Foreman Management Employee Other
Reading                      361             603        241     570        907     1804   154
Listening music              488             715        285     554        853     1879   150
Cinema                       181             307        169     365        640     1016    92
Show                          84             189        132     264        533      734    68
Exhibition                    92             208        138     326        604      717    74
Computer                     147             307        203     351        606      915    80
Sport                        163             296        187     361        595      867    75
Walking                      320             516        209     443        632     1295   112
Travelling                   165             314        189     398        720      932    89
Playing music                 69             105         78     152        288      430    52
Collecting                    69             129         51      85        120      287    16
Volunteering                  54             114         69     168        263      377    40
Mechanic                     268             629        249     381        553      867    90
Gardening                    266             501        162     347        501      968    92
Knitting                     147              98         31     111        116      634    43
Cooking                      314             435        156     313        465     1302    91
Fishing                      110             223         66      94        103      168    19
TV                            78             138         56     127        206      331    34

The values passed in for each loop is a string i.e. 'hob' signifies each value of the rownames,

> with(hobbies, "Reading")
[1] "Reading"

It doesn't return the value of the column 'Reading' when we wrap with hobbies although, directly we can do this

> head(with(hobbies, Reading))
[1] 1 1 1 1 1 0
Levels: 0 1

or with [ or [[

> head(hobbies[, "Reading"])
[1] 1 1 1 1 1 0
Levels: 0 1
> head(hobbies[["Reading"]])
[1] 1 1 1 1 1 0
Levels: 0 1
  • Related