Set up "Sum" label on addmargins function

Let the following table:

x <- sample(1:2, 100, replace = T)
tabela <- table(x)


To which I add fields

> addmargins(tabela)
  1   2 Sum 
 51  49 100 


However, I would like to change the label "Sum" to "Total". How to do it?

My current workaround is to run addmargins

to get the source code of the function, copy it into my script and change the line "Sum"

to "Total"

, but I think there is a smarter way to accomplish this.


source to share

1 answer

A quick look at the source addmargin

will reveal that when it explicitly passed a function through its argument FUN=

, it names the marginal column, overriding the name of the bundled function.

A quick solution is to pass in a function that sums the items, but has a name that you would like to print there.

Total <- sum
addmargins(tabela, FUN = Total)
# x
#     1     2 Total 
#    49    51   100 




All Articles