如何从r中的两个双向表创建一个三向表

时间:2014-02-19 05:47:08

标签: r

我有两个双向表

male = as.table(matrix(c(60, 100, 20, 50), ncol=2))
colnames(male) = c("success", "failure")
rownames(male) = c("treat1", "treat2")

female = as.table(matrix(c(40, 10, 80, 30), ncol=2))
colnames(female) = c("success", "failure")
rownames(female) = c("treat1", "treat2")

如何创建一个名为“threeway”的三向表,以便我可以使用

调用“男性”
threeway[,,1] 

并使用

呼叫女性
threeway[,,2]

2 个答案:

答案 0 :(得分:4)

abind

一样

require(abind)
a<-abind(male,female,rev.along=0,make.names=T)

> a[,,1]
       success failure
treat1      60      20
treat2     100      50
> a[,,2]
       success failure
treat1      40      80
treat2      10      30

答案 1 :(得分:0)

我只想找到一种方法,只使用基本命令:

output = array(as.vector(c(male, female)), dim=c(2,2,2))
dimnames(output) = list(Treatment=c("A", "B"), 
Outcome=c("success", "failure"), Gender=c("male", "female"))

, , Gender = male

         Outcome
Treatment success failure
        A      60      20
        B     100      50

, , Gender = female

         Outcome
Treatment success failure
        A      40      80
        B      10      30