Learn R Programming

memisc (version 0.99.31.7)

contract: Contract data into pattern-frequency format

Description

contract() contracts data into pattern-frequency format, similar to a contatenation of table() (or xtabs) and as.data.frame(). Yet it uses much less memory if patterns are sparse, because it does not create rows for patterns that do not occur.

Usage

contract(x,...)
# S3 method for data.frame
contract(x,by=NULL, weights=NULL,name="Freq",
    force.name=FALSE,sort=FALSE,drop.na=TRUE,...)
# S3 method for data.set
contract(x,by=NULL, weights=NULL,name="Freq",
    force.name=FALSE,sort=FALSE,drop.na=TRUE,...)

Value

If x is a data fame, the value of contract() is also a data frame. If it is a "data.set" object, the result is also a

"data.set" object.

Arguments

x

an object of class "data.frame" or "data.set".

by

the formula or a vector of variable names (quoted or not quoted). Specifies the patterns (and optionally weights). If by is a formula, then the right-hand side specifies the variables the value patterns of which are counted. If the left-hand side of the formula is (the name of) a numeric vector, its values are used as weights (in which case the weights argument will be ignored.) If the left-hand side of the formula is (the name of) a factor, counts are computed in separate columns for each of its levels.

weights

a numeric vector of weights or NULL.

name

a character string, the name of the variable that containts the frequency counts of the value patterns.

force.name

a logical value, defaults to FALSE. If TRUE and the left-hand side of by formula is a factor, the names of the columns with the counts are combinations of the labels of the factor levels and the argument of name; if FALSE, the column names are created from the labels of the factor levels only.

sort

a logical value, defaults to FALSE. If TRUE, the resulting data set is sorted by the variables that define the patterns. If FALSE, the row of the resulting data frame or data set are ordered according to the occurrence of the patterns.

drop.na

a logical value, defaults to TRUE. If FALSE, patterns that involve NA are included in the resulting data frame or data set.

...

further arguments, passed to methods or ignored.

Examples

Run this code
iris_ <- sample(iris,size=nrow(iris),replace=TRUE)
w <- rep(1,nrow(iris_))
contract(iris[4:5])
contract(iris[4:5],sort=TRUE)
contract(iris[4:5],weights=w,sort=TRUE)
contract(iris,by=c(Petal.Width,Species),sort=TRUE)
contract(iris,by=~Petal.Width+Species)
contract(iris,by=w~Species)

library(MASS)
contract(housing,
         by=Sat~Infl+Type+Cont,
         weights=Freq)

contract(housing,
         by=Sat~Infl+Type+Cont,
         weights=Freq,
         name="housing",force.name=TRUE
         )

Run the code above in your browser using DataLab