[R-gregmisc-commits] r2126 - in pkg/gtools: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Apr 22 18:08:25 CEST 2016


Author: warnes
Date: 2016-04-22 18:08:24 +0200 (Fri, 22 Apr 2016)
New Revision: 2126

Modified:
   pkg/gtools/R/na.replace.R
   pkg/gtools/man/na.replace.Rd
Log:
na.replace() now accepts a function providing the replcement value.

Modified: pkg/gtools/R/na.replace.R
===================================================================
--- pkg/gtools/R/na.replace.R	2016-03-30 15:35:48 UTC (rev 2125)
+++ pkg/gtools/R/na.replace.R	2016-04-22 16:08:24 UTC (rev 2126)
@@ -1,5 +1,8 @@
-na.replace <- function(x, replace)
+na.replace <- function(x, replace, ...)
 {
+  if(is.function(replace))
+    replace <- replace(x, ...)
+  
   x[is.na(x)] <- replace
   x
 }

Modified: pkg/gtools/man/na.replace.Rd
===================================================================
--- pkg/gtools/man/na.replace.Rd	2016-03-30 15:35:48 UTC (rev 2125)
+++ pkg/gtools/man/na.replace.Rd	2016-04-22 16:08:24 UTC (rev 2126)
@@ -7,11 +7,12 @@
     Replace missing values
 }
 \usage{
-na.replace(x, replace)
+na.replace(x, replace, ...)
 }
 \arguments{
-  \item{x}{vector possibly contining missing (\code{NA}) values.}
-  \item{replace}{scalar replacement value}
+  \item{x}{vector possibly contining missing (\code{NA}) values}
+  \item{replace}{either a scalar replacement value, or a function returning a scalar value}
+  \item{...}{Optional arguments to be passed to \code{replace}}
 }
 \details{
    This is a convenience function that is the same as
@@ -29,6 +30,11 @@
 }
 \examples{
    x <- c(1,2,3,NA,6,7,8,NA,NA)
+   
+   # Replace with a specified value
    na.replace(x, '999')
+   
+   # Replace with the calculated median
+   na.replace(x, median, na.rm=TRUE)    
 }
 \keyword{ manip }



More information about the R-gregmisc-commits mailing list