aaa
aaa

Reputation: 251

How to combine two tables in R?

This is what I have:

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")

what I want is to combine the two tables above to form the table below:

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")

Also, I wonder if there is some way to let R output the value of I and d in the table according the I and d defined instead of input manually.

Thanks for helping me.

Upvotes: 1

Views: 4857

Answers (1)

akrun
akrun

Reputation: 886938

If you have two vectors and want to get the result (as mentioned in the comments)

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

Update

Regarding combining the two 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)),]

or another way to rearrange the rows would be

 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

Upvotes: 3

Related Questions