How to create a table with sub totals for every row and column in R?

R ProgrammingServer Side ProgrammingProgramming

The sub totals for every row and column are actually called marginal sums. Therefore, we can use addmargins function to our table to get the sub totals for every row and column.

For example, if we have table called TABLE then we can add the sub totals by using the command given below −

TABLE<-addmargins(TABLE,c(1,2),sum)

Check out the examples given below to understand how it works.

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,1)
y1<-rpois(20,5)
df1<-data.frame(x1,y1)
df1

Output

The following dataframe is created −

   x1 y1
1  0  5
2  1  6
3  0  3
4  1  4
5  3  6
6  2  4
7  3  6
8  0  4
9  1  5
10 0  2
11 2  5
12 1  2
13 0  7
14 2  4
15 0  6
16 2  3
17 0  2
18 1  5
19 2  5
20 0  5

To create a table with sub totals for every row and column, add the following code to the above snippet −

x1<-rpois(20,1)
y1<-rpois(20,5)
df1<-data.frame(x1,y1)
table1<-table(df1$x1,df1$y1)
table1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

   2  3  4  5  6  7
0  2  1  1  2  1  1
1  1  0  1  2  1  0
2  0  1  2  2  0  0
3  0  0  0  0  2  0

To create a table with sub totals for every row and column, add the following code to the above snippet −

table1<-addmargins(table1,c(1,2),sum)
Margins computed over dimensions
in the following order:
1:
2:
table1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

    2  3  4  5  6  7  sum
0   2  1  1  2  1  1  8
1   1  0  1  2  1  0  5
2   0  1  2  2  0  0  5
3   0  0  0  0  2  0  2
sum 3  2  4  6  4  1 20

Example 2

Following snippet creates a sample data frame −

x2<-rpois(20,5)
y2<-rpois(20,5)
df2<-data.frame(x2,y2)
df2

Output

The following dataframe is created −

   x2 y2
1  1  7
2  7  6
3  5  3
4  4  3
5  6  3
6  4  3
7  4  4
8  4  7
9  4  5
10 4  6
11 7  7
12 8  9
13 4  7
14 6  6
15 3  6
16 7  5
17 3  3
18 4  0
19 6  7
20 5  4

To create a table with sub totals for every row and column, add the following code to the above snippet −

x2<-rpois(20,5)
y2<-rpois(20,5)
df2<-data.frame(x2,y2)
table2<-table(df2$x2,df2$y2)
table2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

   0  3  4  5  6  7 9
1  0  0  0  0  0  1 0
3  0  1  0  0  1  0 0
4  1  2  1  1  1  2 0
5  0  1  1  0  0  0 0
6  0  1  0  0  1  1 0
7  0  0  0  1  1  1 0
8  0  0  0  0  0  0 1

To create a table with sub totals for every row and column, add the following code to the above snippet −

table2<-addmargins(table2,c(1,2),sum)
Margins computed over dimensions
in the following order:
1:
2:
table2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

    0  3  4  5  6  7  9 sum
1   0  0  0  0  0  1  0  1
3   0  1  0  0  1  0  0  2
4   1  2  1  1  1  2  0  8
5   0  1  1  0  0  0  0  2
6   0  1  0  0  1  1  0  3
7   0  0  0  1  1  1  0  3
8   0  0  0  0  0  0  1  1
sum 1  5  2  2  4  5  1 20
raja
Updated on 03-Nov-2021 07:07:25

Advertisements