Open In App

addmargins() Function in R

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be discussing the addmargins() function with its working examples in the R programming language.

addmargin() function

The addmargins function in R is simply used to add arbitrary margins on a multidimensional table or array.

Syntax: addmargins(A, margin = seq_along(dim(A)), FUN = sum, quiet = FALSE)

Parameters:

  • A: This represents the array or table.
  • margin: This is a vector of dimensions over which the margins are to be formed.
  • FUN: This is a list of the same length as the margin parameter, where each element in the list is either a function or a list of functions.
  • quiet: This takes a logical value, which suppresses the message and describes the order in which the margins were computed.

Returns:

This function returns a table or array with the same number of dimensions as the array or table passed to it, but with extra levels of the dimensions given as a value to the margin parameter.

Method 1: Put Sum Margins to Contingency Table Using addmargins() Function

In this approach to put a sum margin to a contingency table, the user needs to call the addmargin() function which is the base R function, here the addmargins function will add the sum of each row and column to the margins of a table created and passed to the function with the special argument of the function used FUN which is passed to sum in this case in the R programming language.

Example:

In this example, we have first created the data frame, and further, with the use of the table function we have converted this data frame to the table, then with the call of the addmargin function and its special parameter FUN set to sum we have added the sum of each row and column to the margins of the created table in R language.

R




# Creating data
data <- data.frame(x = c("A","A","A","A",
                         "B","B","C","D",
                         "D","D","E","E","E")
                   ,y=letters[1:13])
  
# converting the dataframe to table
my_table <- table(data) 
  
# Apply addmargins function
table_sum <- addmargins(my_table, FUN = sum)      
table_sum


Output:

     y
x      a  b  c  d  e  f  g  h  i  j  k  l  m sum
  A    1  1  1  1  0  0  0  0  0  0  0  0  0   4
  B    0  0  0  0  1  1  0  0  0  0  0  0  0   2
  C    0  0  0  0  0  0  1  0  0  0  0  0  0   1
  D    0  0  0  0  0  0  0  1  1  1  0  0  0   3
  E    0  0  0  0  0  0  0  0  0  0  1  1  1   3
  sum  1  1  1  1  1  1  1  1  1  1  1  1  1  13

Method 2: Put Mean Margins to Contingency Table Using addmargins() Function

In this approach to put a mean margin to a contingency table, the user needs to call the addmargin() function which is the base R function, here the addmargins function will add the mean of each row and column to the margins of a table created and passed to the function with the special argument of the function used FUN which is passed to mean in this case in the R programming language.

Example:

In this example, we have first created the data frame, and further, with the use of the table function we have converted this data frame to the table, then with the call of the addmargin function and its special parameter FUN set to mean we have added the mean of each row and column to the margins of the created table in R language.

R




# Creating data
data <- data.frame(x = c("A","A","A","A","B",
                         "B","C","D","D","D",
                         "E","E","E"),
                   y = letters[1:13])
  
# converting the dataframe to table
my_table <- table(data) 
  
# Apply addmargins function
table_mean <- addmargins(my_table, FUN = mean)      
table_mean


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads