Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize "Sum" label on addmargins function

Let the following table:

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

To which I add margins

> addmargins(tabela)
x
  1   2 Sum 
 51  49 100 

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

My current workaround is to run addmargins to get the function's source code, copy it to my script and change the string "Sum" to "Total", but I imagine there is a cleverer way to accomplish this.

like image 827
Waldir Leoncio Avatar asked Feb 11 '23 21:02

Waldir Leoncio


1 Answers

A quick look at addmargin's source code will show that, when it's explicitly passed a function via it's FUN= argument, it names the marginal column by deparsing the supplied function's name.

A quick solution, then, is to pass in a function that sums the elements but has the name that you'd like to have printed there.

Total <- sum
addmargins(tabela, FUN = Total)
# x
#     1     2 Total 
#    49    51   100 
like image 78
Josh O'Brien Avatar answered Feb 18 '23 21:02

Josh O'Brien