Debian Codegraph @

man/contract.Rd

raw
1 \name{contract}
2 \alias{contract}
3 \alias{contract.data.frame}
4 \alias{contract.data.set}
5 \title{Contract data into pattern-frequency format}
6 \description{
7 \code{contract()} contracts data into pattern-frequency format, similar
8 to a contatenation of \code{table()} (or \code{xtabs}) and
9 \code{as.data.frame()}. Yet it uses much less memory if patterns
10 are sparse, because it does not create rows for patterns that do not occur.
11 }
12 \usage{
13 contract(x,\dots)
14 \method{contract}{data.frame}(x,by=NULL, weights=NULL,name="Freq",
15 force.name=FALSE,sort=FALSE,drop.na=TRUE,\dots)
16 \method{contract}{data.set}(x,by=NULL, weights=NULL,name="Freq",
17 force.name=FALSE,sort=FALSE,drop.na=TRUE,\dots)
18 }
19 \arguments{
20 \item{x}{
21 an object of class \code{"data.frame"} or \code{"data.set"}.
22 }
23 \item{by}{the formula or a vector of variable names (quoted or not quoted).
24 Specifies the patterns (and optionally weights).
25 If \code{by} is a formula, then the right-hand side specifies the
26 variables the value patterns of which are counted.
27 If the left-hand side of the formula is (the name of) a numeric
28 vector, its values are used as weights (in which case the
29 \code{weights} argument will be ignored.) If the left-hand side of
30 the formula is (the name of) a factor, counts are computed in
31 separate columns for each of its levels.
32 }
33 \item{weights}{a numeric vector of weights or \code{NULL}.}
34 \item{name}{a character string, the name of the variable that
35 containts the frequency counts of the value patterns.}
36 \item{force.name}{a logical value, defaults to \code{FALSE}. If
37 \code{TRUE} and the left-hand side of \code{by} formula is a factor,
38 the names of the columns with the counts are combinations of the
39 labels of the factor levels and the argument of \code{name}; if
40 \code{FALSE}, the column names are created from the labels of the
41 factor levels only.
42 }
43 \item{sort}{a logical value, defaults to \code{FALSE}. If \code{TRUE},
44 the resulting data set is sorted by the variables that define the
45 patterns. If \code{FALSE}, the row of the resulting data frame or
46 data set are ordered according to the occurrence of the patterns.
47 }
48 \item{drop.na}{a logical value, defaults to \code{TRUE}. If
49 \code{FALSE}, patterns that involve \code{NA} are included in the
50 resulting data frame or data set.}
51 \item{\dots}{further arguments, passed to methods or ignored.}
52 }
53 \value{
54 If \code{x} is a data fame, the value of \code{contract()} is also a
55 data frame. If it is a \code{"data.set"} object, the result is also a
56 \code{"data.set"} object.
57 }
58 \examples{
59 iris_ <- sample(iris,size=nrow(iris),replace=TRUE)
60 w <- rep(1,nrow(iris_))
61 contract(iris[4:5])
62 contract(iris[4:5],sort=TRUE)
63 contract(iris[4:5],weights=w,sort=TRUE)
64 contract(iris,by=c(Petal.Width,Species),sort=TRUE)
65 contract(iris,by=~Petal.Width+Species)
66 contract(iris,by=w~Species)
67
68 library(MASS)
69 contract(housing,
70 by=Sat~Infl+Type+Cont,
71 weights=Freq)
72
73 contract(housing,
74 by=Sat~Infl+Type+Cont,
75 weights=Freq,
76 name="housing",force.name=TRUE
77 )
78 }