R: table 的参数是一个包含变量名的变量,但不是这样解释的?
R: Argument to table is a variable containing variable name, but is not interpreted as such?
我在循环中调用 table
,所以变量名必须在变量本身中。以下可重现代码:
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
不知何故,hob
的长度被取为1,所以在数据框hobbies
的上下文中不解释?怎么了?
我们可以用[
代替with
for(hob in rownames(htable))
htable[hob, ] <- table(hobbies[, hob],
hobbies[, "Profession"])[2,]
-输出
> 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
每个循环传入的值是一个字符串,即'hob'表示行名的每个值,
> with(hobbies, "Reading")
[1] "Reading"
当我们用 hobbies
换行时,它不会 return 列 'Reading' 的值,尽管我们可以直接这样做
> head(with(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
> head(hobbies[["Reading"]])
[1] 1 1 1 1 1 0
Levels: 0 1
我在循环中调用 table
,所以变量名必须在变量本身中。以下可重现代码:
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
不知何故,hob
的长度被取为1,所以在数据框hobbies
的上下文中不解释?怎么了?
我们可以用[
代替with
for(hob in rownames(htable))
htable[hob, ] <- table(hobbies[, hob],
hobbies[, "Profession"])[2,]
-输出
> 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
每个循环传入的值是一个字符串,即'hob'表示行名的每个值,
> with(hobbies, "Reading")
[1] "Reading"
当我们用 hobbies
换行时,它不会 return 列 'Reading' 的值,尽管我们可以直接这样做
> head(with(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
> head(hobbies[["Reading"]])
[1] 1 1 1 1 1 0
Levels: 0 1