为什么下标超出范围错误?

时间:2019-04-04 11:37:21

标签: r

我正在跟踪课程中的一些代码,但使用自己的数据。现在我收到“下标超出范围”错误,但无法在线找到解决方案

尝试在线搜索解决方案,尝试使用类别变量而不是连续的crp2。 (TB是二进制的)

检查crp2与结核病对数几率之间的关系是否为线性(逻辑回归的假设)

# 1. create a cross tabulation of crp2 and TB status  
tb_by_crp2 <- table(crp2,Culpos)

# 2. output the frequencies of TB status by crp2 
freq_table <- prop.table(tb_by_crp2, margin = 1)

# 3. calculate the odds of having TB 
odds <- freq_table[, "yes"]/freq_table[, "no"]
  

[.default(freq_table,“是”)中的错误:下标超出范围

我没想到会出错

  

dput(tb_by_crp2)   结构(c(31L,3L,3L,1L,3L,5L,1L,2L,0L,3L,2L,1L,   1L,2L,1L,0L,1L,0L,0L,1L,2L,0L,1L,0L,1L,1L,1L,0L,   0L,1L,2L,1L,0L,0L,1L,0L,0L,2L,2L,0L,1L,0L,1L,1L,   1L,1L,0L,0L,0L,0L,0L,0L,0L,0L,1L,1L,1L,1L,0L,0L,   1L,1L,2L,0L,0L,1L,1L,0L,1L,0L,0L,1L,1L,0L,0L,2L,   0L,0L,1L,0L,0L,1L,0L,0L,3L,2L,1L,0L,0L,0L,0L,0L,   0L,1L,1L,1L,0L,1L,0L,1L,1L,0L,1L,1L,0L,0L,1L,0L,   1L,0L,0L,0L,1L,3L,0L,0L,0L,1L,1L,0L,1L,1L,1L,0L,   1L,1L,2L,0L,1L,1L,0L,1L,2L,1L,1L,2L,1L,1L,1L,0L,   1L,0L,0L,1L,1L,2L,1L,0L,1L,1L,0L,1L,1L,0L,1L,1L,   1L,0L,1L,1L,0L,1L,1L,0L,1L,1L,0L,1L,1L,2L).Dim = c(85L,   2L),.Dimnames = structure(list(crp2 = c(“ 5”,“ 6”,“ 7”,“ 8”,   “ 9”,“ 10”,“ 12”,“ 13”,“ 14”,“ 15”,“ 16”,“ 18”,“ 19”,“ 20”,“ 21”,   “ 22”,“ 24”,“ 25”,“ 26”,“ 27”,“ 28”,“ 30”,“ 31”,“ 33”,“ 34”,“ 35”,   “ 36”,“ 37”,“ 38”,“ 39”,“ 40”,“ 42”,“ 44”,“ 46”,“ 48”,“ 51”,“ 55”,   “ 56”,“ 58”,“ 60”,“ 62”,“ 68”,“ 71”,“ 72”,“ 73”,“ 76”,“ 78”,“ 81”,   “ 85”,“ 89”,“ 90”,“ 91”,“ 92”,“ 95”,“ 96”,“ 97”,“ 99”,“ 102”,   “ 103”,“ 104”,“ 106”,“ 107”,“ 109”,“ 114”,“ 119”,“ 127”,“ 128”,   “ 131”,“ 132”,“ 136”,“ 141”,“ 148”,“ 152”,“ 156”,“ 157”,“ 159”,   “ 162”,“ 165”,“ 168”,“ 173”,“ 181”,“ 193”,“ 196”,“ 199”,“ 300”   ),Culpos = c(“ 0”,“ 1”)),.Names = c(“ crp2”,“ Culpos”)),class =“ table”)

1 个答案:

答案 0 :(得分:0)

根据您提供的数据,freq_table的列名称不是“ yes”和“ no”,而是01

因此,如果将其更改为列索引,则可以正常工作:



odds <- freq_table[, 1]/freq_table[, 2]

> head(odds, n = 10)
   5    6    7    8    9   10   12   13   14   15 
15.5  3.0  Inf  Inf  Inf  Inf  Inf  Inf  0.0  3.0 

希望能解决您的问题。