如何在R中组合两个表?

时间:2014-11-27 15:49:51

标签: r knitr

这就是我所拥有的:

library(knitr)
table_MSE_EB <- matrix(c(0.0053,0.1140,0.1542,0.1677,0.0049,0.1177,0.1570,0.1762,0.0040,0.1211,0.1578,0.1803),4,3)
colnames(table_MSE_EB) <- c("I=10", "I=20", "I=50")
rownames(table_MSE_EB) <- c("d=2", "d=3", "d=5", "d=10")
kable(head(table_MSE_EB), format = "pandoc", caption = "Err for EB")

table_MSE_MLE <- matrix(c(0.034,0.0388,0.0544,0.0847,0.0263,0.0332,0.0445,0.0778,0.0193,0.0263,0.0376,0.0742),4,3)
colnames(table_MSE_MLE) <- c("I=10", "I=20", "I=50")
rownames(table_MSE_MLE) <- c("d=2", "d=3", "d=5", "d=10")
kable(head(table_MSE_MLE), format = "pandoc", caption = "Err for MLE")

我想要的是将上面的两个表组合起来形成下表:

table <- matrix(c(0.0053,0.034,0.1140,0.0388,0.1542,0.0544,0.1677,0.0847,0.0049,0.0263,0.1177,0.0332,0.1570,0.0445,0.1762,0.0778,0.004,0.0193,0.1211,0.0263,0.1578,0.0376,0.1803,0.0742),8,3)
colnames(table) <- c("I=10", "I=20", "I=50")
rownames(table) <- c("d=2(EB)", "d=2(MLE)", "d=3(EB)", "d=3(MLE)", "d=5(EB)","d=5(MLE)",   "d=10(EB)", "d=10(MLE)")
kable(head(table), format = "pandoc", caption = "Err for MLE/EB")

另外,我想知道是否有某种方法让R根据I和d定义输出I和d的值,而不是手动输入。

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

如果您有两个向量并且想要获得结果(如评论中所述)

a <- c(5,6,2,4,4,3) 
b <-  c(10,13,12,12, 15,15)
c(rbind(a,b))
#[1]  5 10  6 13  2 12  4 12  4 15  3 15

更新

关于组合两个tables

row.names(table_MSE_EB) <- paste0(row.names(table_MSE_EB), "(EB)")
row.names(table_MSE_MLE) <- paste0(row.names(table_MSE_MLE), "(MLE)")
tableNew <- rbind(table_MSE_EB, table_MSE_MLE)
tableNew1 <-  tableNew[c(matrix(1:nrow(tableNew),nrow=2, byrow=TRUE)),]

或重新排列rows的其他方法是

 n <- nrow(table_MSE_EB)
 indx <-  c(rbind(seq(1,n), seq(n+1, 2*n)))
 tableNew1 <-  tableNew[indx,]

 kable(head(tableNew1), format='pandoc', caption='Err for MLE/EB')


 #Table: Err for MLE/EB

 #              I=10     I=20     I=50
 #---------  -------  -------  -------
 #d=2(EB)     0.0053   0.0049   0.0040
 #d=2(MLE)    0.0340   0.0263   0.0193
 #d=3(EB)     0.1140   0.1177   0.1211
 #d=3(MLE)    0.0388   0.0332   0.0263
 #d=5(EB)     0.1542   0.1570   0.1578
 #d=5(MLE)    0.0544   0.0445   0.0376