From marius.hofert at math.ethz.ch  Thu Dec 27 23:05:52 2012
From: marius.hofert at math.ethz.ch (Marius Hofert)
Date: Thu, 27 Dec 2012 23:05:52 +0100
Subject: [Rd] Suggestion: 'method' slot for expand.grid() (incl. diffs)
Message-ID: <871ueb6thb.fsf@sklar.v.cablecom.net>

Dear expeRts,

The order in which the variables vary in expand.grid() is often unintuitive. I
would like to suggest a 'method' slot for expand.grid() which requires only very
little changes (100% backward compatible) and which allows one to control this
order. Please find attached diffs against R-devel.

Cheers,

Marius



### ./src/library/base/R/expand.grid.R #########################################

--- expand.grid.R	2012-12-27 22:37:29.000000000 +0100
+++ expand.grid2.R	2012-12-27 22:41:00.331979950 +0100
@@ -16,7 +16,8 @@
 #  A copy of the GNU General Public License is available at
 #  http://www.r-project.org/Licenses/

-expand.grid <- function(..., KEEP.OUT.ATTRS = TRUE, stringsAsFactors = TRUE)
+expand.grid <- function(..., KEEP.OUT.ATTRS = TRUE, stringsAsFactors = TRUE,
+                        method = c("decreasing", "increasing"))
 {
     ## x should either be a list or a set of vectors or factors
     nargs <- length(args <- list(...))
@@ -26,7 +27,9 @@
     if(nargs == 0L) return(as.data.frame(list()))
     ## avoid classed args such as data frames: cargs <- args
     cargs <- vector("list", nargs)
-    iArgs <- seq_len(nargs)
+    seqArgs <- seq_len(nargs)
+    method <- match.arg(method)
+    iArgs <- if(method=="decreasing") seqArgs else rev(seqArgs)
     nmc <- paste0("Var", iArgs)
     nm <- names(args)
     if(is.null(nm))


### ./src/library/base/man/expand.grid.Rd ######################################

--- expand.grid.Rd	2012-12-27 22:38:13.000000000 +0100
+++ expand.grid2.Rd	2012-12-27 22:46:53.103964121 +0100
@@ -6,7 +6,8 @@
 \name{expand.grid}
 \title{Create a Data Frame from All Combinations of Factors}
 \usage{
-expand.grid(\dots, KEEP.OUT.ATTRS = TRUE, stringsAsFactors = TRUE)
+expand.grid(\dots, KEEP.OUT.ATTRS = TRUE, stringsAsFactors = TRUE,
+            method = c("decreasing", "increasing"))
 }
 \alias{expand.grid}
 \arguments{
@@ -15,6 +16,15 @@
     attribute (see below) should be computed and returned.}
   \item{stringsAsFactors}{logical specifying if character vectors are
     converted to factors.}
+  \item{method}{method slot for how the resulting data frame is
+    presented. Available are:
+    \describe{
+      \item{"decreasing"}{the default; the variability of the variables
+	is decreasing in the column number.}
+      \item{"increasing"}{the variability of the variables
+	is increasing in the column number.}
+    }
+  }
 }
 \description{
   Create a data frame from all combinations of the supplied vectors or
@@ -52,6 +62,8 @@

 expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50),
             sex = c("Male","Female"))
+expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50),
+            sex = c("Male","Female"), method = "increasing")

 x <- seq(0, 10, length.out = 100)
 y <- seq(-1, 1, length.out = 20)



[Rd] Suggestion: 'method' slot for expand.grid() (incl. diffs)
Marius Hofert