Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Last active August 29, 2015 14:00
Show Gist options
  • Save MrFlick/00e2c589a2fa4b6d91f2 to your computer and use it in GitHub Desktop.
Save MrFlick/00e2c589a2fa4b6d91f2 to your computer and use it in GitHub Desktop.
Expand.Grid.R: A slight modification to expand.grid() such that when you pass data.frames in, the rows are kept together as tuples rather than being split apart
Expand.Grid<-function (..., stringsAsFactors = TRUE)
{
nargs <- length(args <- list(...))
if (!nargs)
return(as.data.frame(list()))
if (nargs == 0L)
return(as.data.frame(list()))
Names <- function(x) {if(!is.null(names(x))) names(x) else rep("",length(x))}
Paste <- function(...) {a<-list(...); r<-do.call("paste", c(list(sep="."),
a[sapply(a, function(x) !is.character(x) || any(nzchar(x)))]));
nx <- max(sapply(a, length))
if (length(r)) return(rep(r, length.out=nx)) else return(rep("", nx))
}
contribcols <- sapply(args, function(x) ifelse(class(x)=="data.frame", ncol(x), 1))
outargs <- sum(contribcols)
cargs <- vector("list", outargs)
nmc <- paste0("Var", seq.int(sum(contribcols)))
nm <- unlist(lapply(seq_along(args), function(x) if(class(args[[x]])=="data.frame") {
Paste(Names(args)[x], Names(args[[x]])) } else {Names(args)[x]}))
if (is.null(nm))
nm <- nmc
else if (any(ng0 <- !nzchar(nm)))
nm[ng0] <- nmc[ng0]
names(cargs) <- make.unique(make.names(nm))
rep.fac <- 1L
d <- sapply(args, function(x) ifelse(class(x)=="data.frame", nrow(x), length(x)))
orep <- prod(d)
if (orep == 0L) {
i<-1
for (a in seq_along(args)) {
if (contribcols[a]==1) {
args[[a]]=list(a)
}
for(j in seq_len(contribcols[a])) {
cargs[[i]] <- args[[a]][[j]][FALSE]
i <- i+1
}
}
} else {
i<-1
for (a in seq_along(args)) {
nx <- d[a]
orep <- orep/nx
x<-args[[a]]
if (contribcols[a]==1) {
x<-list(x)
}
for(j in seq_len(contribcols[a])) {
y <- x[[j]]
y <- y[rep.int(rep.int(seq_len(nx), rep.int(rep.fac,
nx)), orep)]
if (stringsAsFactors && !is.factor(y) && is.character(y))
y <- factor(y, levels = unique(y))
cargs[[i]] <- y
i <- i+1
}
rep.fac <- rep.fac * nx
}
}
rn <- .set_row_names(as.integer(prod(d)))
structure(cargs, class = "data.frame", row.names = rn)
}
#same
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"))
#different
expand.grid(data.frame(a=c("ac","dc"), b=c("+","-")), g=1:3) #<-error
Expand.Grid(data.frame(a=c("ac","dc"), b=c("+","-")), g=1:3)
#different
Expand.Grid(a=c("ac","dc"), b=c("+","-"), g=1:3)
Expand.Grid(data.frame(a=c("ac","dc"), b=c("+","-")), g=1:3) #no ac/- or dc/+ combinations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment