Hello, I have a variable named 'x' defined inside a function, which may conflict with a variable name supplied in the argument to the function. What is the best practice to avoid this conflict? foo <- function(df) { x <- df[, 1, drop = FALSE] dfOut <- data.frame(df, x) dfOut } Data <- data.frame(x = c(1, 2), y = c(3, 4)) foo(Data) x y x.1 1 1 3 1 2 2 4 2 The following solution doesn't look nice to me? foo <- function(df) { if (any(names(df) == 'x')) stop("x cannot be a variable name in the data frame") x <- df[, 1, drop = FALSE] dfOut <- data.frame(df, x) dfOut } Thanks! Axel. [[alternative HTML version deleted]]
Axel, The solution you propose looks fine to me, if an error is the outcome that you want in such a situation. Were you hoping for a different outcome? Would you, for example, prefer that the "x" in the data frame be given a different name, rather than the "x" in the function? Jean On Thu, Oct 15, 2015 at 11:42 AM, Axel Urbiz <axel.urbiz at gmail.com> wrote:> Hello, > > I have a variable named 'x' defined inside a function, which may conflict > with a variable name supplied in the argument to the function. What is the > best practice to avoid this conflict? > > foo <- function(df) { > x <- df[, 1, drop = FALSE] > dfOut <- data.frame(df, x) > dfOut > > } > Data <- data.frame(x = c(1, 2), y = c(3, 4)) > foo(Data) > > x y x.1 > 1 1 3 1 > 2 2 4 2 > > The following solution doesn't look nice to me? > > foo <- function(df) { > if (any(names(df) == 'x')) > stop("x cannot be a variable name in the data frame") > x <- df[, 1, drop = FALSE] > dfOut <- data.frame(df, x) > dfOut > > } > > Thanks! > Axel. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
My strategy is to be specific about the names of columns at the top level. As I see it, letting functions internally come up with their own column names makes fragile code. foo <- function(df, newColName ) { x <- setNames( df[, 1, drop = FALSE], newColName ) dfOut <- data.frame(df, x) dfOut } Data <- data.frame(x = c(1, 2), y = c(3, 4)) foo(Data, "z") #at this point you know what names would collide and can choose wisely --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On October 15, 2015 9:42:42 AM PDT, Axel Urbiz <axel.urbiz at gmail.com> wrote:>Hello, > >I have a variable named 'x' defined inside a function, which may >conflict >with a variable name supplied in the argument to the function. What is >the >best practice to avoid this conflict? > >foo <- function(df) { > x <- df[, 1, drop = FALSE] > dfOut <- data.frame(df, x) > dfOut > >} >Data <- data.frame(x = c(1, 2), y = c(3, 4)) >foo(Data) > > x y x.1 >1 1 3 1 >2 2 4 2 > >The following solution doesn't look nice to me? > >foo <- function(df) { > if (any(names(df) == 'x')) > stop("x cannot be a variable name in the data frame") > x <- df[, 1, drop = FALSE] > dfOut <- data.frame(df, x) > dfOut > >} > >Thanks! >Axel. > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.