[R-gregmisc-commits] r2148 - in pkg/gdata: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jun 5 21:20:03 CEST 2017
Author: warnes
Date: 2017-06-05 21:20:02 +0200 (Mon, 05 Jun 2017)
New Revision: 2148
Modified:
pkg/gdata/NAMESPACE
pkg/gdata/R/update.data.frame.R
pkg/gdata/R/update.list.R
pkg/gdata/man/update.list.Rd
Log:
Drop 'update.data.frame' until there is time to work on it.
Modified: pkg/gdata/NAMESPACE
===================================================================
--- pkg/gdata/NAMESPACE 2017-05-24 22:09:12 UTC (rev 2147)
+++ pkg/gdata/NAMESPACE 2017-06-05 19:20:02 UTC (rev 2148)
@@ -44,7 +44,7 @@
trimSum,
unmatrix,
update.list,
- update.data.frame,
+ #update.data.frame,
upperTriangle,
"upperTriangle<-",
wideByFactor,
@@ -171,4 +171,4 @@
# update methods for list, data.frame
S3method(update, list)
-S3method(update, data.frame)
+#S3method(update, data.frame)
Modified: pkg/gdata/R/update.data.frame.R
===================================================================
--- pkg/gdata/R/update.data.frame.R 2017-05-24 22:09:12 UTC (rev 2147)
+++ pkg/gdata/R/update.data.frame.R 2017-06-05 19:20:02 UTC (rev 2148)
@@ -1,33 +1,48 @@
-# 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
-}
+# # This function replace rows in 'object' by corresponding rows in 'new' that have
+# # the same value for 'by'
+# update.data.frame <- function(object,
+# new,
+# by,
+# by.object=by,
+# by.new=by,
+# append=TRUE,
+# verbose=TRUE,
+# ...)
+# {
+# retval <- object
+#
+# if(length(by.object)>1)
+# stop("'by.object' can specify at most one column")
+#
+# if(length(by.new)>1)
+# stop("'by.new' can specify at most one column")
+#
+#
+# object.by <- object[[by.object]]
+# new.by <- new [[by.new ]]
+#
+# matches.object <- match(new.by, object.by)
+# matches.new <- which(!is.na(matches.object))
+# nomatch.new <- which(is.na(matches.object))
+# matches.object <- matches.object[!is.na(matches.object)]
+#
+# if(length(matches.object)>0)
+# retval[matches.object, ] <- new[matches.new,]
+#
+# if(length(nomatch.new) && append)
+# retval <- rbind(retval, new[nomatch.new,])
+#
+# if(verbose)
+# {
+# cat("\n")
+# cat("Number of rows in object:", nrow(object), "\n")
+# cat("Number of rows in new :", nrow(new), "\n")
+# cat("\n")
+# cat("Number of rows replaced :", length(matches.object), "\n")
+# cat("Number of rows appended :", length(nomatch.new), "\n")
+# cat("\n")
+# cat("Number of rows in result:", nrow(retval), "\n")
+# cat("\n")
+# }
+# retval
+# }
Modified: pkg/gdata/R/update.list.R
===================================================================
--- pkg/gdata/R/update.list.R 2017-05-24 22:09:12 UTC (rev 2147)
+++ pkg/gdata/R/update.list.R 2017-06-05 19:20:02 UTC (rev 2148)
@@ -1,7 +1,10 @@
## 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, ...)
+update.list <- function(object,
+ new,
+ unnamed=FALSE,
+ ...)
{
retval <- object
@@ -17,4 +20,5 @@
}
retval
-}
\ No newline at end of file
+}
+
Modified: pkg/gdata/man/update.list.Rd
===================================================================
--- pkg/gdata/man/update.list.Rd 2017-05-24 22:09:12 UTC (rev 2147)
+++ pkg/gdata/man/update.list.Rd 2017-06-05 19:20:02 UTC (rev 2148)
@@ -1,30 +1,49 @@
\name{update.list}
+%%\alias{update}
\alias{update.list}
-\alias{update.data.frame}
+%%\alias{update.data.frame}
\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.
+ 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, ...)
-\method{update}{data.frame}(object, new, unnamed=FALSE, ...)
+%%update(object,
+%% ...)
+\method{update}{list}(object,
+ new,
+ unnamed=FALSE,
+ ...)
+%%\method{update}{data.frame}(object,
+%% new,
+%% by,
+%% by.object=by,
+%% by.new=by,
+%% append=TRUE,
+%% verbose=TRUE,
+%% 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}}
+ \item{object}{Object to be updated.}
+ \item{new}{List or dataframe containing new elements.}
+ \item{unnamed}{Logical. If \code{TRUE}, unnamed elements
+ of \code{new} will be appended to \code{object}.}
+%% \item{by, by.object, by,new}{For dataframe objects, rows of \code{object}
+%% will be replaced by rows of \code{new} that have the same value
+%% for this \em{one} column. (See examples below for an example.) }
+%% \item{verbose}{Display details of changes performed.}
\item{...}{optional method arguments (ignored)}
}
\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.
+\value{A new list constructed from the elements of \code{object} by merging the elements of \code{old} and \code{new} with named items of \code{new} replacing the corresponding elements of \code{old}. Unnamed elements of \code{new} will be appened unless \code{unnamed=FALSE}.
+
+%%For dataframe objects, entire columns are replaced unless \code{by}, \code{by.object}, or \code{by.new} are supplied, in which case replacement only occures for rows with matching values for the columns listed by these parameters. The single argument \code{by} can by used to provides column names for that are common to both \code{old} and \code{new} objects, while \code{by.old} and \cody{by.new} specify column names specific to each object.
}
\author{
Gregory R. Warnes \email{greg at warnes.net}
@@ -43,6 +62,16 @@
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
+
+%%data(iris)
+%%iris$Species <- as.character(iris$Species)
+%%df.old <- cbind(iris[1:5,], letters=1:5)
+%%df.new <- df.old[3:5,]
+%%df.new$Petal.Width <- df.new$Petal.Width + 0.1
+%%
+%%df.old
+%%df.new
+%%update(df.old, df.new, by=c(letters))
}
\keyword{data}
\keyword{manip}
More information about the R-gregmisc-commits
mailing list