# having problem while performing sum of rows and columns

m <- matrix(c(200, 100, 0, 80, 200, 20, 20, 80, 100, 0, 20, 180), nrow = 3, ncol = 4)

rownames(m) <- c("0-5", "5-10", "10-30")
colnames(m) <- c("1-10", "10-21", "21-40", "40-60")
t <- cbind(m, c(200+ 80+20+ 0), c(100+200+80+20), c(0+20+100+180))
View(t)

anyone can help me to perform same as like in the picture. Any other way to do like this? I am confused to perform same as like in the picture. above is code .

Here is one approach to get the basic matrix.

``````m <- matrix(c(200, 100, 0, 80, 200, 20, 20, 80, 100, 0, 20, 180), nrow = 3, ncol = 4)

rownames(m) <- c("0-5", "5-10", "10-30")
colnames(m) <- c("1-10", "10-21", "21-40", "40-60")
m
#>       1-10 10-21 21-40 40-60
#> 0-5    200    80    20     0
#> 5-10   100   200    80    20
#> 10-30    0    20   100   180
RowSum <- rowSums(m)
ColSum <- colSums(m)
m2 <- cbind(m, RowSum)
m2
#>       1-10 10-21 21-40 40-60 RowSum
#> 0-5    200    80    20     0    300
#> 5-10   100   200    80    20    400
#> 10-30    0    20   100   180    300
m3 <- rbind(m2, c(ColSum, sum(ColSum)))
m3
#>       1-10 10-21 21-40 40-60 RowSum
#> 0-5    200    80    20     0    300
#> 5-10   100   200    80    20    400
#> 10-30    0    20   100   180    300
#>        300   300   200   200   1000
``````

Created on 2019-10-02 by the reprex package (v0.2.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.