I created a table as following:

mymatrix3=matrix(c(29,54,44,77,62,131,36,67), nrow=4, ncol = 2, byrow = TRUE)
colnames(mymatrix3) <- c("Right Direction", "Wrong Direction")
rownames(mymatrix3) <-c("Northeast", "North Central", "South", "West")
mymatrix3

sample_dist = as.array(prop.table(mymatrix3,2))

It gives me: mymatrix3 which is the opinion of people of different regions over the direction of government:

                   Right Direction Wrong Direction
Northeast                  29              54
North Central              44              77
South                      62             131
West                       36              67

sample_dist:

              Right Direction Wrong Direction
Northeast           0.1695906       0.1641337
North Central       0.2573099       0.2340426
South               0.3625731       0.3981763
West                0.2105263       0.2036474

However, what I want is the sum of two columns over the row margin of table. I don't want to separate class of people. I want to aggregate them and then calculate the prop. What can I do?

2 Answers

1
Clemsang On Best Solutions

You can sum rows then calculate the table :

prop.table(rowSums(mymatrix3,2))

> Northeast     North Central South         West 
  0.166         0.242         0.386         0.206 
1
s_t On

If you want simply the sum of rows of the columns (should work with any number of columns), what about:

cbind(sample_dist, total = rowSums(sample_dist))
              Right Direction Wrong Direction     total
Northeast           0.1695906       0.1641337 0.3337244
North Central       0.2573099       0.2340426 0.4913525
South               0.3625731       0.3981763 0.7607494
West                0.2105263       0.2036474 0.4141737

In case you need their weight on their total:

cbind(sample_dist, total = rowSums(sample_dist), perc = prop.table(rowSums(sample_dist)))

              Right Direction Wrong Direction     total      perc
Northeast           0.1695906       0.1641337 0.3337244 0.1668622
North Central       0.2573099       0.2340426 0.4913525 0.2456762
South               0.3625731       0.3981763 0.7607494 0.3803747
West                0.2105263       0.2036474 0.4141737 0.2070869