[R-gregmisc-users] SF.net SVN: r-gregmisc:[1309] trunk/gdata
Brought to you by:
warnes
From: <gg...@us...> - 2008-12-31 13:28:05
|
Revision: 1309 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1309&view=rev Author: ggorjan Date: 2008-12-31 13:28:02 +0000 (Wed, 31 Dec 2008) Log Message: ----------- New function nPairs that gives the number of variable pairs in a data.frame or a matrix. Added Paths: ----------- trunk/gdata/R/nPairs.R trunk/gdata/inst/unitTests/runit.nPairs.R trunk/gdata/man/nPairs.Rd Added: trunk/gdata/R/nPairs.R =================================================================== --- trunk/gdata/R/nPairs.R (rev 0) +++ trunk/gdata/R/nPairs.R 2008-12-31 13:28:02 UTC (rev 1309) @@ -0,0 +1,49 @@ +### nPairs.R +###------------------------------------------------------------------------ +### What: Number of variable pairs - code +### $Id$ +### Time-stamp: <2008-12-30 18:29:58 ggorjan> +###------------------------------------------------------------------------ + +nPairs <- function(x, margin=FALSE, names=TRUE, abbrev=TRUE, ...) +{ + ## --- Setup --- + if(!is.data.frame(x) & !is.matrix(x)) stop("'x' must be a data.frame or a matrix") + k <- ncol(x) + if(!margin) { + ret <- matrix(nrow=k, ncol=k) + } else { + ret <- matrix(nrow=k, ncol=k + 1) + } + + ## --- Count --- + diag(ret)[1:k] <- apply(X=x, MARGIN=2, FUN=function(x) sum(!is.na(x))) + for(i in 1:k) { + for(j in i:k) { + ret[i, j] <- ret[j, i] <- sum(!is.na(x[, i]) & !is.na(x[, j])) + if(margin) { + if(i == 1) { + ret[i, (k + 1)] <- ret[1, 1] + } else { + ret[i, (k + 1)] <- sum(rowSums(!is.na(x[, c(1:i)])) == i) + } + } + } + } + + ## --- Names --- + if(names) { + tmp <- colnames(x) + if(abbrev) tmp <- as.character(abbreviate(tmp, ...)) + rownames(ret) <- tmp + if(margin) { + colnames(ret) <- c(tmp, "all") + } else { + colnames(ret) <- tmp + } + } + ret +} + +###------------------------------------------------------------------------ +### nPairs.R ends here Property changes on: trunk/gdata/R/nPairs.R ___________________________________________________________________ Added: svn:keywords + Added: trunk/gdata/inst/unitTests/runit.nPairs.R =================================================================== --- trunk/gdata/inst/unitTests/runit.nPairs.R (rev 0) +++ trunk/gdata/inst/unitTests/runit.nPairs.R 2008-12-31 13:28:02 UTC (rev 1309) @@ -0,0 +1,56 @@ +### runit.nPairs.R +###------------------------------------------------------------------------ +### What: Number of variable pairs - unit tests +### $Id$ +### Time-stamp: <2008-12-30 18:24:59 ggorjan> +###------------------------------------------------------------------------ + +### {{{ --- Test setup --- + +if(FALSE) { + library("RUnit") + library("gdata") +} + +### }}} +### {{{ --- nPairs --- + +test.nPairs <- function() +{ + ## 'x' must be a data.frame or a matrix + x <- rpois(100, lambda=10) + checkException(nPairs(x=x)) + checkException(nPairs(x=table(x))) + + test <- data.frame(V1=c(1, 2, 3, 4, 5), + V2=c(NA, 2, 3, 4, 5), + V3=c(1, NA, NA, NA, NA), + V4=c(1, 2, 3, NA, NA)) + testCheck <- matrix(as.integer(c(5, 4, 1, 3, + 4, 4, 0, 2, + 1, 0, 1, 1, + 3, 2, 1, 3)), + nrow=4, ncol=4, byrow=TRUE) + + testCheckNames <- testCheck + colnames(testCheckNames) <- rownames(testCheckNames) <- colnames(test) + + checkIdentical(nPairs(x=test), testCheckNames) + checkIdentical(nPairs(x=test, names=FALSE), testCheck) + checkIdentical(nPairs(x=as.matrix(test)), testCheckNames) + checkIdentical(nPairs(x=as.matrix(test), names=FALSE), testCheck) + + testCheck <- cbind(testCheckNames, as.integer(c(5, 4, 0, 0))) + colnames(testCheck) <- c(colnames(test), "all") + checkIdentical(nPairs(x=test, margin=TRUE), testCheck) +} + +### }}} +### {{{ Dear Emacs +### Local variables: +### folded-file: t +### end: +### }}} + +###------------------------------------------------------------------------ +### runit.nPairs.R ends here Property changes on: trunk/gdata/inst/unitTests/runit.nPairs.R ___________________________________________________________________ Added: svn:keywords + Added: trunk/gdata/man/nPairs.Rd =================================================================== --- trunk/gdata/man/nPairs.Rd (rev 0) +++ trunk/gdata/man/nPairs.Rd 2008-12-31 13:28:02 UTC (rev 1309) @@ -0,0 +1,73 @@ +% nPairs.Rd +%-------------------------------------------------------------------------- +% What: Number of variable pairs - help +% $Id$ +% Time-stamp: <2008-12-30 18:30:11 ggorjan> +%-------------------------------------------------------------------------- + +\name{nPairs} + +\alias{nPairs} + +\concept{pairs} + +\title{Number of variable pairs} + +\description{ + +\code{nPairs} counts the number of pairs between variables. + +} + +\usage{ +nPairs(x, margin=FALSE, names=TRUE, abbrev=TRUE, ...) +} + +\arguments{ + \item{x}{data.frame or a matrix} + \item{margin}{logical, calculate the cumulative number of \dQuote{pairs}} + \item{names}{logical, add row/col-names to the output} + \item{abbrev}{logical, abbreviate names} + \item{\ldots}{other arguments passed to \code{\link{abbreviate}}} +} + +\value{ + +Matrix of order \eqn{k}, where \eqn{k} is the number of columns in \code{x}. +Values in a matrix represent the number of pairs between columns/variables in +\code{x}. If \code{margin=TRUE}, the number of columns is \eqn{k+1} and the +last column represents the cumulative number of pairing all variables. + +} + +\author{Gregor Gorjanc} + +\seealso{\code{\link{abbreviate}}} + +\examples{ + +## Test data +test <- data.frame(V1=c(1, 2, 3, 4, 5), + V2=c(NA, 2, 3, 4, 5), + V3=c(1, NA, NA, NA, NA), + V4=c(1, 2, 3, NA, NA)) + +## Number of variable pairs +nPairs(x=test) + +## Without names +nPairs(x=test, names=FALSE) + +## Longer names +colnames(test) <- c("Variable1", "Variable2", "Variable3", "Variable4") +nPairs(x=test) + +## Margin +nPairs(x=test, margin=TRUE) + +} + +\keyword{misc} + +%-------------------------------------------------------------------------- +% nPairs.Rd ends here This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |