The R Project SVN R

Rev

Rev 77798 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 77798 Rev 85061
Line 1... Line 1...
1
% File src/library/base/man/marginSums.Rd
1
% File src/library/base/man/marginSums.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2020 R Core Team
3
% Copyright 1995-2023 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{marginSums}
6
\name{marginSums}
7
\alias{marginSums}
7
\alias{marginSums}
8
\alias{margin.table}
8
\alias{margin.table}
9
\title{Compute table margins}
9
\title{Compute Table Margins}
10
\description{
10
\description{
11
  For a contingency table in array form, compute the sum of table
11
  For a contingency table in array form, compute the sum of table
12
  entries for a given margin or set of margins.
12
  entries for a given margin or set of margins.
13
}
13
}
14
\usage{
14
\usage{
15
marginSums(x, margin = NULL)
15
marginSums(x, margin = NULL)
16
margin.table(x, margin = NULL)
16
margin.table(x, margin = NULL)
17
}
17
}
18
\arguments{
18
\arguments{
19
  \item{x}{an array}
19
  \item{x}{an array, usually a \code{\link{table}}.}
20
  \item{margin}{a vector giving the margins to compute sums for.
20
  \item{margin}{a vector giving the margins to compute sums for.
21
    E.g., for a matrix \code{1} indicates rows, \code{2} indicates
21
    E.g., for a matrix \code{1} indicates rows, \code{2} indicates
22
    columns, \code{c(1, 2)} indicates rows and columns.
22
    columns, \code{c(1, 2)} indicates rows and columns.
23
    When \code{x} has named dimnames, it can be a character vector
23
    When \code{x} has named \code{\link{dimnames}}, it can be a character vector
24
    selecting dimension names.}
24
    selecting dimension names.}
25
}
25
}
26
\seealso{
26
\seealso{
-
 
27
  \code{\link{rowSums}} and \code{\link{colSums}} for similar functionality.
-
 
28
 
27
  \code{\link{proportions}} and \code{\link{addmargins}}.
29
  \code{\link{proportions}} and \code{\link{addmargins}}.
28
}
30
}
29
\value{
31
\value{
30
  The relevant marginal table, or just the sum of all entries if \code{margin} has length zero.  The class of \code{x} is copied to the
32
  The relevant marginal table, or just the sum of all entries if \code{margin} has length zero.  The class of \code{x} is copied to the
31
  output table if \code{margin} is non-NULL.
33
  output table if \code{margin} is non-NULL.
32
}
34
}
33
\note{\code{margin.table} is an earlier name, retained for back-compatibility.}
35
\note{\code{margin.table} is an earlier name, retained for back-compatibility.}
34
\author{Peter Dalgaard}
36
\author{Peter Dalgaard}
35
\examples{
37
\examples{
36
m <- matrix(1:4, 2)
38
m <- matrix(1:4, 2)
37
marginSums(m, 1)
39
marginSums(m, 1)  # = rowSums(m)
38
marginSums(m, 2)
40
marginSums(m, 2)  # = colSums(m)
39
 
-
 
40
 
41
 
41
DF <- as.data.frame(UCBAdmissions)
42
DF <- as.data.frame(UCBAdmissions)
42
tbl <- xtabs(Freq ~ Gender + Admit, DF)
43
tbl <- xtabs(Freq ~ Gender + Admit, DF)
43
 
44
tbl
44
marginSums(tbl, "Gender")
45
marginSums(tbl, "Gender")  # a 1-dim "table"
45
proportions(tbl, "Gender")
46
rowSums(tbl)               # a numeric vector
46
}
47
}
47
\keyword{array}
48
\keyword{array}