From 7d3f31c0b0fa0ec7350bcd40ab63a336155ee032 Mon Sep 17 00:00:00 2001 From: "Qian, Hai" Date: Wed, 16 Apr 2014 21:53:29 +0000 Subject: [PATCH] Bug fix: correctly handle column name which is a substring of another longer column name --- DESCRIPTION | 2 +- R/margins.R | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5753e9c..49e4da5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: PivotalR Type: Package Title: R front-end to PostgreSQL and Pivotal (Greenplum) database, wrapper for MADlib -Version: 0.1.15.27 +Version: 0.1.15.29 Date: 2014-03-10 Author: Predictive Analytics Team at Pivotal Inc. , with contributions from Data Scientist Team at Pivotal Inc. diff --git a/R/margins.R b/R/margins.R index 5a3040a..67c1d89 100644 --- a/R/margins.R +++ b/R/margins.R @@ -42,14 +42,18 @@ Vars <- function(model) ## Add `\"\"` quotes to vars using model.vars .add.quotes <- function(vars, model.vars) { - as.vector(sapply(vars, - function(v) { - for (var in model.vars) - v <- gsub(var, paste("`\"", var, "\"`", sep = ""), v) - v <- gsub("([^`]|^)\"([^\\[\\]]*)\"\\[(\\d+)\\]([^`]|$)", - "(`\"\\2\"[\\3]`)", v) - v - })) + no.conflict.names <- sapply(seq_along(model.vars), function(i) .unique.string()) + parse.var <- function (v) { + for (i in order(nchar(model.vars), decreasing = TRUE)) + v <- gsub(model.vars[i], paste("`\"", no.conflict.names[i], "\"`", sep = ""), + v, perl = TRUE) + for (i in seq_along(model.vars)) + v <- gsub(no.conflict.names[i], model.vars[i], v, perl = TRUE) + v <- gsub("([^`]|^)\"([^\\[\\]]*)\"\\[(\\d+)\\]([^`]|$)", + "(`\"\\2\"[\\3]`)", v, perl = TRUE) + v + } + as.vector(sapply(vars, parse.var)) } ## ---------------------------------------------------------------------- @@ -204,7 +208,6 @@ Vars <- function(model) ## ------- Add quotes ------- mvars <- Vars(model) model.vars <- .add.quotes(gsub("`", "", model.vars), mvars) - model.vars <- lapply(model.vars, function(x) eval(parse(text=paste("quote(", x, ")", sep = ""))))