[R-gregmisc-users] SF.net SVN: r-gregmisc: [1019] trunk/gtools
Brought to you by:
warnes
From: <wa...@us...> - 2006-11-27 21:34:11
|
Revision: 1019 http://svn.sourceforge.net/r-gregmisc/?rev=1019&view=rev Author: warnes Date: 2006-11-27 13:34:07 -0800 (Mon, 27 Nov 2006) Log Message: ----------- Add smartbind() to list of exported functions, and add corresponding documentation file. Modified Paths: -------------- trunk/gtools/DESCRIPTION trunk/gtools/NAMESPACE trunk/gtools/NEWS trunk/gtools/R/smartbind.R Added Paths: ----------- trunk/gtools/man/smartbind.Rd Modified: trunk/gtools/DESCRIPTION =================================================================== --- trunk/gtools/DESCRIPTION 2006-11-27 20:52:33 UTC (rev 1018) +++ trunk/gtools/DESCRIPTION 2006-11-27 21:34:07 UTC (rev 1019) @@ -1,8 +1,8 @@ Package: gtools Title: Various R programming tools Description: Various R programming tools -Depends: R (>= 1.9.0) -Version: 2.2.4 +Depends: R +Version: 2.3.0 Author: Gregory R. Warnes. Includes R source code and/or documentation contributed by Ben Bolker and Thomas Lumley Maintainer: Gregory R. Warnes <wa...@bs...> Modified: trunk/gtools/NAMESPACE =================================================================== --- trunk/gtools/NAMESPACE 2006-11-27 20:52:33 UTC (rev 1018) +++ trunk/gtools/NAMESPACE 2006-11-27 21:34:07 UTC (rev 1019) @@ -24,6 +24,7 @@ running, scat, setTCPNoDelay, + smartbind, sprint, strmacro ) Modified: trunk/gtools/NEWS =================================================================== --- trunk/gtools/NEWS 2006-11-27 20:52:33 UTC (rev 1018) +++ trunk/gtools/NEWS 2006-11-27 21:34:07 UTC (rev 1019) @@ -1,11 +1,19 @@ -CHANGES IN gtools 2.2.3 ------------------------ +gtools 2.3.0 +------------ +- Update email address for Greg + +- Add new 'smartbind' function, which combines data frames + efficiently, even if they have different column names. + +gtools 2.2.3 +------------ + - setTCPNoDelay now compiles & works properly on Windows -CHANGES IN gtools 2.2.2 ------------------------ +gtools 2.2.2 +------------ - src/setTCPNoDelay.c: Add C source code for setTCPNoDelay. @@ -14,8 +22,8 @@ - Updated Greg's email address. -CHANGES IN gtools 2.2.1 ------------------------ +gtools 2.2.1 +------------ - New function 'addLast' that adds functions to R's .Last() so that they will be executed when R is terminating. @@ -23,8 +31,8 @@ - New function setTCPNoDelay() that allows the TCP_NODELAY flag to be changed on socket objects. -CHANGES IN gtools 2.1.0 ------------------------ +gtools 2.1.0 +------------ - Added assert.R (and documentation) Modified: trunk/gtools/R/smartbind.R =================================================================== --- trunk/gtools/R/smartbind.R 2006-11-27 20:52:33 UTC (rev 1018) +++ trunk/gtools/R/smartbind.R 2006-11-27 21:34:07 UTC (rev 1019) @@ -10,7 +10,13 @@ data <- list(...) if(is.null(names(data))) names(data) <- as.character(1:length(data)) - data <- lapply(data, function(x) if(is.matrix(x) || is.data.frame(x)) x else data.frame(as.list(x)) ) + data <- lapply(data, + function(x) + if(is.matrix(x) || is.data.frame(x)) + x + else + data.frame(as.list(x)) + ) #retval <- new.env() retval <- list() @@ -33,7 +39,10 @@ { if( !(col %in% names(retval))) { - if(verbose) cat("Start:", start, " End:", end, " Column:", col, "\n", sep="") + if(verbose) cat("Start:", start, + " End:", end, + " Column:", col, + "\n", sep="") if(class(block[,col])=="factor") newclass <- "character" else @@ -53,38 +62,3 @@ return(retval) } -testfun <- function(n=10,s=10) - { - names <- unlist(outer(LETTERS,letters, paste, sep="")) - - Z <- list() - for(i in 1:n) - { - X <- data.frame(A=sample(letters,s,replace=T), - B=1:s, - C=rnorm(s) ) - names(X) <- c("A",sample(names[1:s*2],2,replace=F)) - Z[[i]] <- X - } - - - - #ut2 <- unix.time( - # retval2 <- do.call("rbind",Z) - # ) - # - #cat("rbind used",ut2[3], "seconds.\n") - - - ut1 <- unix.time( - retval1 <- do.call("smartbind",Z) - ) - - cat("smartbind used",ut1[3], "seconds.\n") - - - invisible(retval1) - } - - - Added: trunk/gtools/man/smartbind.Rd =================================================================== --- trunk/gtools/man/smartbind.Rd (rev 0) +++ trunk/gtools/man/smartbind.Rd 2006-11-27 21:34:07 UTC (rev 1019) @@ -0,0 +1,74 @@ +\name{smartbind} +\alias{smartbind} +\title{Efficient rbind of data framesy, even if the column names don't match} +\description{ + Efficient rbind of data frames, even if the column names don't match +} +\usage{ +smartbind(...) +} +\arguments{ + \item{\dots}{Data frames to combine} +} +\value{ + The returned data frame will contain: + \item{columns}{all columns present in any provided data frame} + \item{rows}{a set of rows from each provided data frame, with values + in columns not present in the given data frame filled with missing + (\code{NA}) values.} + The data type of columns will be preserved, as long as all data frames + with a given column name agree on the data type of that column. If + the data frames disagree, the column will be converted into a + character strings. The user will need to coerce such character + columns into an appropriate type. +} +\author{Gregory R. Warnes \email{wa...@bs...}} +\seealso{ \code{\link{rbind}}, \code{\link{cbind}} } +\examples{ + + df1 <- data.frame(A=1:10, B=LETTERS[1:10], C=rnorm(10) ) + df2 <- data.frame(A=11:20, D=rnorm(10), E=letters[1:10] ) + + # rbind would fail +\dontrun{ + rbind(df1, df2) + # Error in match.names(clabs, names(xi)) : names do not match previous + # names: + # D, E +} + # but smartbind combines them, appropriately creating NA entries + smartbind(df1, df2) + +\dontshow{ + n=10 # number of data frames to create + s=10 # number of rows in each data frame + + # create a bunch of column names + names <- LETTERS[2:5] + + # create a list 'Z' containing 'n' data frames, each with 3 columns + # and 's' rows. The first column is always named 'A', but the other + # two have a names randomly selected from 'names' + + Z <- list() + for(i in 1:n) + { + X <- data.frame(A=sample(letters,s,replace=T), + B=letters[1:s], + C=rnorm(s) ) + colnames(X) <- c("A",sample(names,2,replace=F)) + Z[[i]] <- X + } + + # Error in match.names(clabs, names(xi)) : names do not match + # previous names: E + + # But smartbind will 'do the right thing' + df <- do.call("smartbind",Z) + df +} +} +% Add one or more standard keywords, see file 'KEYWORDS' in the +% R documentation directory. +\keyword{manip} + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |