addmargins               package:stats               R Documentation

_P_u_t_s _A_r_b_i_t_r_a_r_y _M_a_r_g_i_n_s _o_n _M_u_l_t_i_d_i_m_e_n_s_i_o_n_a_l _T_a_b_l_e_s _o_r _A_r_r_a_y_s

_D_e_s_c_r_i_p_t_i_o_n:

     For a given table one can specify which of the classifying factors
     to expand by one or more levels to hold margins to be calculated. 
     One may for example form sums and means over the first dimension
     and medians over the second.  The resulting table will then have
     two extra levels for the first dimension and one extra level for
     the second.  The default is to sum over all margins in the table. 
     Other possibilities may give results that depend on the order in
     which the margins are computed.  This is flagged in the printed
     output from the function.

_U_s_a_g_e:

       addmargins(A, margin = 1:length(dim(A)), FUN = sum, quiet = FALSE)

_A_r_g_u_m_e_n_t_s:

       A: table or array.  The function uses the presence of the
          '"dim"' and '"dimnames"' attributes of 'A'.

  margin: vector of dimensions over which to form margins.  Margins are
          formed in the order in which dimensions are specified in
          'margin'.

     FUN: list of the same length as 'margin', each element of the list
          being either a function or a list of functions.  Names of the
          list elements will appear as levels in dimnames of the
          result. Unnamed list elements will have names constructed: 
          the name of a function or a constructed name based on the
          position in the table.

   quiet: logical which suppresses the message telling the order in
          which the margins were computed.

_D_e_t_a_i_l_s:

     If the functions used to form margins are not commutative the
     result depends on the order in which margins are computed. 
     Annotation of margins is done via naming the 'FUN' list.

_V_a_l_u_e:

     A table or array with the same number of dimensions as 'A', but
     with extra levels of the dimensions mentioned in 'margin'.  The
     number of levels added to each dimension is the length of the
     entries in 'FUN'.  A message with the order of computation of
     margins is printed.

_A_u_t_h_o_r(_s):

     Bendix Carstensen, Steno Diabetes Center & Department of
     Biostatistics, University of Copenhagen, <URL:
     http://www.biostat.ku.dk/~bxc>, autumn 2003. Margin naming
     enhanced by Duncan Murdoch.

_S_e_e _A_l_s_o:

     'table', 'ftable', 'margin.table'.

_E_x_a_m_p_l_e_s:

     Aye <- sample(c("Yes", "Si", "Oui"), 177, replace = TRUE)
     Bee <- sample(c("Hum", "Buzz"), 177, replace = TRUE)
     Sea <- sample(c("White", "Black", "Red", "Dead"), 177, replace = TRUE)
     (A <- table(Aye, Bee, Sea))
     addmargins(A)

     ftable(A)
     ftable(addmargins(A))

     # Non-commutative functions - note differences between resulting tables:
     ftable(addmargins(A, c(1,3),
            FUN = list(Sum = sum, list(Min = min, Max = max))))
     ftable(addmargins(A, c(3,1),
            FUN = list(list(Min = min, Max = max), Sum = sum)))

     # Weird function needed to return the N when computing percentages
     sqsm <- function(x) sum(x)^2/100
     B <- table(Sea, Bee)
     round(sweep(addmargins(B, 1, list(list(All = sum, N = sqsm))), 2,
                 apply(B, 2, sum)/100, "/"), 1)
     round(sweep(addmargins(B, 2, list(list(All = sum, N = sqsm))), 1,
                 apply(B, 1, sum)/100, "/"), 1)

     # A total over Bee requires formation of the Bee-margin first:
     mB <-  addmargins(B, 2, FUN = list(list(Total = sum)))
     round(ftable(sweep(addmargins(mB, 1, list(list(All = sum, N = sqsm))), 2,
                        apply(mB,2,sum)/100, "/")), 1)

     ## Zero.Printing table+margins:
     set.seed(1)
     x <- sample( 1:7, 20, replace=TRUE)
     y <- sample( 1:7, 20, replace=TRUE)
     tx <- addmargins( table(x, y) )
     print(tx, zero.print = ".")

