[R-gregmisc-commits] r2074 - in pkg/gdata: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Feb 3 21:15:02 CET 2016


Author: warnes
Date: 2016-02-03 21:15:02 +0100 (Wed, 03 Feb 2016)
New Revision: 2074

Added:
   pkg/gdata/R/update.data.frame.R
   pkg/gdata/R/update.list.R
   pkg/gdata/man/update.list.Rd
Log:
Add update() methods for lists and data frames

Added: pkg/gdata/R/update.data.frame.R
===================================================================
--- pkg/gdata/R/update.data.frame.R	                        (rev 0)
+++ pkg/gdata/R/update.data.frame.R	2016-02-03 20:15:02 UTC (rev 2074)
@@ -0,0 +1,33 @@
+# This function replace rows in 'x' by corresponding rows in 'y' the have 
+# the same value for 'by'
+update.data.frame <- function(x, y, by, by.x=by, by.y=by, append=TRUE, verbose=TRUE, ...)
+{
+  retval <- x
+  x.by <- x[[by.x]]
+  y.by <- y[[by.y]]
+  
+  matches.x <- match(y.by, x.by)
+  matches.y <- which(!is.na(matches.x))
+  nomatch.y <- which(is.na(matches.x))
+  matches.x <- matches.x[!is.na(matches.x)]
+  
+  if(length(matches.x)>0)
+    retval[matches.x, ] <- y[matches.y,]
+  
+  if(length(nomatch.y) && append)  
+    retval <- rbind(retval, y[nomatch.y,])
+  
+  if(verbose)
+  {
+    cat("\n")    
+    cat("Number of rows in x     :", nrow(x),           "\n")
+    cat("Number of rows in y     :", nrow(y),           "\n")
+    cat("\n")
+    cat("Number of rows replaced :", length(matches.x), "\n")
+    cat("Number of rows appended :", length(nomatch.y), "\n")
+    cat("\n")
+    cat("Number of rows in result:", nrow(retval),      "\n")
+    cat("\n")
+  }
+  retval
+}

Added: pkg/gdata/R/update.list.R
===================================================================
--- pkg/gdata/R/update.list.R	                        (rev 0)
+++ pkg/gdata/R/update.list.R	2016-02-03 20:15:02 UTC (rev 2074)
@@ -0,0 +1,20 @@
+## this function updates the elements of list 'object' to contain all of the elements
+## of 'new', overwriting elements with the same name, and (optionally) copying unnamed
+## elements.
+update.list <- function(object, new, unnamed=FALSE, ...)
+{
+  retval <- object
+
+  for(name in names(new))
+    retval[[name]] <- new[[name]]
+
+  if(unnamed)
+  {
+    if(is.null(names(new)))
+      names(new) <- rep("", length=length(new))
+    for(i in (1:length(new))[names(new)==""] )
+        retval <- append(retval, new[[i]])
+  }
+
+  retval
+}
\ No newline at end of file

Added: pkg/gdata/man/update.list.Rd
===================================================================
--- pkg/gdata/man/update.list.Rd	                        (rev 0)
+++ pkg/gdata/man/update.list.Rd	2016-02-03 20:15:02 UTC (rev 2074)
@@ -0,0 +1,45 @@
+\name{update.list}
+\alias{update.list}
+\title{
+  Update the elements of a list
+}
+\description{
+ Function to update the elements of a list to contain all of the named elements
+ of a new list, overwriting elements with the same name, and (optionally) copying unnamed
+ elements.
+ }
+\usage{
+update.list(object, new, unnamed=FALSE, ...)
+\method{update}{list}(object, new, unnamed=FALSE, ...)
+}
+\arguments{
+  \item{object}{List to be updated.}
+  \item{new}{List containing new elements.}
+  \item{unnamed}{Logical. If \code{TRUE}, unnamed elements of \code{new} will be appended to \code{object}}
+}
+\note{
+  This method can be called directly, or as via the S3 base method for \code{update}.
+}
+\value{
+A constructed from the elements of \code{object}, with named elements of \code{new} replacing corresponding named elements from \code{object}, and non-corresponding elements of \code{new} appended.   If \code{unnamed=TRUE}, unnamed elements of \code{new} will be appended.
+}
+\author{
+Gregory R. Warnes \email{greg at warnes.net}
+}
+\seealso{
+\code{\link[stats]{update}}, \code{\link[base]{merge}}
+}
+\examples{
+old <- list(a=1,b="red",c=1.37)
+new <- list(b="green",c=2.4)
+
+update(old, new)
+update.list(old,new)  # equivalent
+
+older <- list(a=0, b="orange", 4, 5, 6)
+newer <- list(b="purple", 7, 8, 9)
+update(older, newer)               # ignores unnamed elements of newer
+update(older, newer, unnamed=TRUE) # appends unnamed elements of newer
+}
+\keyword{data}
+\keyword{manip}



More information about the R-gregmisc-commits mailing list