addmargins(A, margin = seq_along(dim(A)), FUN = sum, quiet = FALSE)
A | a multidimensional array or table. |
margin | a vector specifying the index and order of dimensions to add margins. |
FUN | a function or a list of functions to be applied as margins. The length of FUN is either 1 or same as the length of margin. If it is a list, each element can also be a function or a list of functions. |
quiet | a logical value. If TRUE, the order of margins computed over dimensions will not be shown. This argument is effective only when FUN is specified and the number of margins to be added is more than one. |
set.seed(1) x.array <- array(sample.int(10, 24, replace = TRUE), dim = c(4, 2, 3), dimnames = list(Group = c( "A", "B", "C", "D"), Color = c("Red", "Blue"), Year = c("1990", "2000", "2010")) ) addmargins(x.array)# add margins to "Year" and "Group" dimensions with named function "sum" and "mean" addmargins(x.array, margin = c(3, 1), FUN =list(Total = sum, Average = mean) )
# add margins to "Year" and "Group" dimensions with unnamed function "mean", "max" and "min" addmargins(x.array, margin = c(3, 1), FUN =list(sum, list(max, min)) ) # almost same as above, but with the named list "Range" for unnamed "max" and unnamed "min" addmargins(x.array, margin = c(3, 1), FUN =list(sum, Range = list(max, min)) ) # almost same as above, but with the named list "Range" for named "max" and named "min" addmargins(x.array, margin = c(3, 1), FUN =list(sum, Range = list(Max = max, Min = min)) )
# Don't display the message of the order of margins computed over dimensions addmargins(x.array, margin = c(3, 1), FUN =list(Total = sum, Average = mean), quiet = TRUE )
# Example for a table. x <- sample( 1:7, 20, replace=TRUE) y <- sample( 1:7, 20, replace=TRUE) addmargins( table(x, y) )