我有两个尺寸相同的矩阵/表格。基本上,一个是观察到的计数,另一个是我计算过的预期计数。我正在尝试合并条目,并在其旁边的括号中包含观察到的计数与期望计数的矩阵。 我试过使用paste()函数,但是这只会覆盖表中的条目。
答案 0 :(得分:2)
可能不是最好的选择,但是您可以尝试以下方法:
observed <- matrix(1:20, nrow = 5) # if as.matrix() if necessary
expected <- matrix(21:40, nrow = 5)
matrix(paste(as.character(observed), " (", as.character(expected), ")", sep = ""),
nrow = nrow(observed))
或
`dim<-`(sprintf("%d (%d)", observed, expected), dim(observed)) # thank you @markus for pointing that one
# output
[,1] [,2] [,3] [,4]
[1,] "1 (21)" "6 (26)" "11 (31)" "16 (36)"
[2,] "2 (22)" "7 (27)" "12 (32)" "17 (37)"
[3,] "3 (23)" "8 (28)" "13 (33)" "18 (38)"
[4,] "4 (24)" "9 (29)" "14 (34)" "19 (39)"
[5,] "5 (25)" "10 (30)" "15 (35)" "20 (40)"