[R-gregmisc-users] SF.net SVN: r-gregmisc: [978] trunk/gdata
Brought to you by:
warnes
|
From: <wa...@us...> - 2006-08-03 22:26:45
|
Revision: 978 Author: warnes Date: 2006-08-03 15:26:30 -0700 (Thu, 03 Aug 2006) ViewCVS: http://svn.sourceforge.net/r-gregmisc/?rev=978&view=rev Log Message: ----------- Add Gregor Gorjanc's mapFactor() and combineLevels() functions. Modified Paths: -------------- trunk/gdata/DESCRIPTION trunk/gdata/NAMESPACE Added Paths: ----------- trunk/gdata/R/combineLevels.R trunk/gdata/R/mapFactor.R trunk/gdata/man/combineLevels.Rd trunk/gdata/man/mapFactor.Rd Modified: trunk/gdata/DESCRIPTION =================================================================== --- trunk/gdata/DESCRIPTION 2006-08-02 22:21:49 UTC (rev 977) +++ trunk/gdata/DESCRIPTION 2006-08-03 22:26:30 UTC (rev 978) @@ -3,9 +3,8 @@ Description: Various R programming tools for data manipulation Depends: R (>= 1.9.0) Imports: gtools -Version: 2.1.3 -Date: 2005-10-27 +Version: 2.2.0 Author: Gregory R. Warnes. Includes R source code and/or documentation - contributed by Ben Bolker and Thomas Lumley + contributed by Ben Bolker, Gregor Gorjanc, and Thomas Lumley Maintainer: Gregory Warnes <gre...@ur...> License: GPL (version 2 or later) Modified: trunk/gdata/NAMESPACE =================================================================== --- trunk/gdata/NAMESPACE 2006-08-02 22:21:49 UTC (rev 977) +++ trunk/gdata/NAMESPACE 2006-08-03 22:26:30 UTC (rev 978) @@ -3,6 +3,7 @@ Args, aggregate.table, combine, + combineLevels, ConvertMedUnits, drop.levels, elem, @@ -13,7 +14,8 @@ keep, ll, lowerTriangle, - "lowerTriangle<-", + "lowerTriangle<-", + mapFactor, matchcols, nobs, read.xls, Added: trunk/gdata/R/combineLevels.R =================================================================== --- trunk/gdata/R/combineLevels.R (rev 0) +++ trunk/gdata/R/combineLevels.R 2006-08-03 22:26:30 UTC (rev 978) @@ -0,0 +1,26 @@ +## combineLevels.R +###------------------------------------------------------------------------ +## What: Joint levels of given factors +## $Id: combineLevels.R,v 1.1 2006/04/08 01:58:36 ggorjan Exp $ +## Time-stamp: <2006-04-08 03:57:53 ggorjan> +###------------------------------------------------------------------------ + +combineLevels <- function(x, apply=TRUE, drop=FALSE) +{ + if (!is.factor(x)) { + if (sum(!(c("data.frame", "list") %in% class(x))) == 2) + stop(paste(sQuote("x"), "must be a", dQuote("data.frame"), "or a", dQuote("list"))) + if (any(!(unlist((lapply(x, is.factor)))))) + stop(paste("only", dQuote("factors"), "are supported")) + if (drop) x <- lapply(x, factor) + levs <- sort(unique(unlist(lapply(x, levels)))) + if (!apply) return(levs) + return(lapply(x, "levels<-", mapFactor(levs, codes=FALSE))) + } + if (drop) x <- factor(x) + if (!apply) return(levels(x)) + return(x) +} + +###------------------------------------------------------------------------ +## combineLevels.R ends here Added: trunk/gdata/R/mapFactor.R =================================================================== --- trunk/gdata/R/mapFactor.R (rev 0) +++ trunk/gdata/R/mapFactor.R 2006-08-03 22:26:30 UTC (rev 978) @@ -0,0 +1,38 @@ +## mapFactor.R +###------------------------------------------------------------------------ +## What: Get a map of levels in a factor +## $Id: mapFactor.R,v 1.1 2006/03/29 13:47:15 ggorjan Exp ggorjan $ +## Time-stamp: <2006-04-06 01:35:30 ggorjan> +###------------------------------------------------------------------------ + +mapFactor <- function(x, codes=TRUE, sort=TRUE, drop=FALSE, ...) +{ + ## --- Check --- + msg <- "x must be a factor or character" + if (!is.factor(x)) { + if (!is.character(x)) stop(msg) + } + + ## --- Create a map --- + if (is.factor(x)) { # factor + if (drop) x <- factor(x) + nlevs <- nlevels(x) + levs <- levels(x) + if (sort) levs <- sort(levs, ...) + } else { # character + levs <- unique(x) + if (sort) levs <- sort(levs, ...) + nlevs <- length(levs) + } + tmp <- vector("list", nlevs) + names(tmp) <- levs + if (codes) { + tmp[1:nlevs] <- 1:nlevs + } else { + tmp[1:nlevs] <- levs + } + return(tmp) +} + +###------------------------------------------------------------------------ +## mapFactor.R ends here Added: trunk/gdata/man/combineLevels.Rd =================================================================== --- trunk/gdata/man/combineLevels.Rd (rev 0) +++ trunk/gdata/man/combineLevels.Rd 2006-08-03 22:26:30 UTC (rev 978) @@ -0,0 +1,61 @@ +% combineLevels.Rd +%-------------------------------------------------------------------------- +% What: Combine levels of given factors +% $Id: combineLevels.Rd,v 1.1 2006/03/29 13:47:10 ggorjan Exp ggorjan $ +% Time-stamp: <2006-06-27 09:30:42 ggorjan> +%-------------------------------------------------------------------------- + +\name{combineLevels} + +\alias{combineLevels} + +\title{Combine levels of given factors} + +\description{ +\code{combineLevels} combines levels of given factors and applies this +levels to given factors. This eases the work with factors since all +factors have the same levels. +} + +\usage{ +combineLevels(x, apply=TRUE, drop=FALSE) +} + +\arguments{ + \item{x}{data.frame or list, object with factors} + \item{apply}{boolean, apply combined levels to \code{x} or just return + combined levels} + \item{drop}{boolean, drop unused levels} +} + +\value{\code{apply} handles the output. If \code{apply=TRUE} the output + is a modified \code{x}, where all factors have the same set of + levels. If \code{apply=FALSE} only combined levels are returned. +} + +\author{Gregor Gorjanc} + +\seealso{ + \code{\link{factor}}, \code{\link{levels}}, \code{\link[ggmisc]{mapFactor}} +} + +\examples{ + +(f1 <- factor(letters[1:5])) +(f2 <- factor(letters[3:10])) +tmp <- list(f1, f2) +combineLevels(tmp) +combineLevels(tmp, apply=FALSE) + +f1[2] <- NA +f1 <- factor(f1) +tmp <- list(f1, f2) +combineLevels(tmp, apply=FALSE, drop=TRUE) + +} + +\keyword{misc} +\keyword{manip} + +%-------------------------------------------------------------------------- +% combineLevels.Rd ends here Added: trunk/gdata/man/mapFactor.Rd =================================================================== --- trunk/gdata/man/mapFactor.Rd (rev 0) +++ trunk/gdata/man/mapFactor.Rd 2006-08-03 22:26:30 UTC (rev 978) @@ -0,0 +1,81 @@ +% mapFactor.Rd +%-------------------------------------------------------------------------- +% What: Get a map of levels in a factor man page +% $Id: mapFactor.Rd,v 1.1 2006/03/29 13:47:10 ggorjan Exp ggorjan $ +% Time-stamp: <2006-06-27 09:31:03 ggorjan> +%-------------------------------------------------------------------------- + +\name{mapFactor} + +\alias{mapFactor} + +\title{Get a map of levels in a factor} + +\description{ +\code{mapFactor} produces a list with information on levels and internal +integer codes. As such can be conveniently used to store factor map when +one needs to work with internal codes of a factor and later transfrorm +back to factor. +} + +\usage{ +mapFactor(x, codes=TRUE, sort=TRUE, drop=FALSE, ...) +} + +\arguments{ + \item{x}{factor, the object to be mapped} + \item{codes}{boolean, create map with internal codes or with + levels, look into value and examples} + \item{sort}{boolean, sort levels for a character, look into details} + \item{drop}{boolean, drop unused levels of a factor} + \item{...}{additional arguments for \code{sort}} +} + +\details{ + \code{sort} and \code{...} arguments provides possibility to "order" + levels and can only be used for characters and not for factors. +} + +\value{A list with names equal to levels and entries equal to internal +codes, when \code{codes=TRUE}, or entries equal to levels +otherwise. The later case is usefull, when one would like to combine +two factors with different levels. +} + +\author{Gregor Gorjanc} + +\seealso{ + \code{\link{factor}}, \code{\link{levels}}, \code{\link{unclass}}, + \code{\link{attributes}} +} + +\examples{ + +## Example with codes=TRUE +(f <- factor(letters[c(1, 1, 2, 3, 4, 5, 7, 8, 9, 8, 8, 10)])) +map <- mapFactor(f) +int <- as.integer(f) +fNew <- factor(int) +levels(fNew) <- map +fNew + +## Example with codes=FALSE +f1 <- factor(f[1:5]) +f2 <- factor(f[5:length(f)]) +map1 <- mapFactor(f1, codes=FALSE) +map2 <- mapFactor(f2, codes=FALSE) +map <- c(map1, map2) +levels(f1) <- map +levels(f2) <- map +as.integer(f1) +as.integer(f2) + +## x <- unique(map) +## names(x) <- unlist(x) +} + +\keyword{misc} +\keyword{manip} + +%-------------------------------------------------------------------------- +% mapFactor.Rd ends here This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |