0

I have a vector like this :

> agg$RNAtype
 [1] "HnRn" "HyRy" "HnRn" "HyRn" "HnRn" "HnRn" "HnRn" "HnRn" "HnRy" "HnRn" "HnRy"
[12] "HnRn"

Now when I use table function, it gives the following:

> table(agg$RNAtype)

HnRn HnRy HyRn HyRy 
   8    2    1    1 

But I want to make a 2x2 contingency table (in the form of a matrix), where the first row will have values of HnRn and HyRn. And the second row will have values of HnRy and HyRy. How can I do it?

I am ultimately planning to do fisher's exact test and chisquare test.

4

2 回答 2

1
vec <- scan(what="")
1: "HnRn" "HyRy" "HnRn" "HyRn" "HnRn" "HnRn" "HnRn" "HnRn" "HnRy" "HnRn" "HnRy"
12:  "HnRn"
13: 
Read 12 items

看起来您想对每个项目中的第二个和第四个字符进行交叉分类:

> table(substr(vec,4,4), substr(vec,2,2))

    n y
  n 8 1
  y 2 1

这有点神秘,所以使用 table 的标签功能来改进注释:

> tbl <- table(`4th`=substr(vec,4,4), `2nd`=substr(vec,2,2))
> tbl
   2nd
4th n y
  n 8 1
  y 2 1

表对象适合输入fisher.test

> fisher.test(tbl)

    Fisher's Exact Test for Count Data

data:  tbl
p-value = 0.4545
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
   0.03405901 351.00812616
sample estimates:
odds ratio 
  3.463796 
于 2014-07-23T20:12:08.697 回答
0

我认为@BondedDust 的答案更好,但是如果您只想重新排列您拥有的表格,您大概可以这样做

matrix(table(agg$RNAtype),nrow=2)

(正如@Henrik 在上面的评论中指出的那样,这与?fisher.test?chisq.test...中的示例非常相似)

默认情况下,R 以“列优先”顺序构造矩阵,因此这应该以您想要的方式获得条目的顺序。

于 2014-07-23T20:15:20.917 回答