Reputation: 31
I have two matrices/tables of the same dimensions. Basically, one is of observed counts and another is of expected counts in which I've calculated. I'm trying to combine the entries and have a matrix of the observed counts with the expected count in parenthesis next to it. I've tried using the paste() function, however, that just overwrites the entries of my table.
For example, I have two table:
And I want to combine them to look like this:
Upvotes: 0
Views: 43
Reputation: 6768
Probably not the best option but you can try this :
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))
or
`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)"
Upvotes: 2