[R] Accessing specific data.frame columns within function

Clark Kogan kogan.clark at gmail.com
Fri Feb 5 05:13:13 CET 2016


Hello,

I am trying to write a function that adds a few columns to a data.frame. The
function uses the columns in a specific way. For instance, it might take a^2
+ c to produce a column d. Or it might do more complex manipulations that I
don't think I need to discuss here. I want to keep x as a data.frame when I
pass it into the function, as I want to use some data.frame functionality on
x.

Furthermore, I don't want the names in x to have to be specific. I want to
be able to specify which columns the function should treat as "a" and "c".

The way I am currently doing it, is that I pass the names of the columns
that I want to treat as a and c.

f <- function(data,oldnames) {
  newnames <- c("a","c")
  ix <- match(oldnames,names(y))
  names(y)[ix] <- newnames
  y <- subset(y,c==4)
  y$d <- y$a^2 + y$c
  ix <- match(newnames,names(y))
  names(y)[ix] <- oldnames
  y
}

y <- data.frame(k=c(1,1,1),l=c(2,2,5),m=c(4,2,4))
f(y,c("k","m"))

The way that I am doing it does not seem all that elegent or standard
practice. My question is: are there potential problems programming with
data.frames in this way, and are their standard practice methods of
referencing data.frame names that deal with these problems?

Thanks!

Clark

	[[alternative HTML version deleted]]



More information about the R-help mailing list