Skip to content

Environments

3 messages · Philip Whittall, Hans-Peter Suter, Bill Venables

#
A B Z D
1 2 3 4
#
What you are trying to do is make R behave like a macro language, which is not a good idea at all.  Better to use R as it was designed to be used rather than make it behave like something it wasn't.

It also follows that trying to do this is going to be tricky.  Here is one way:

RENAME <- function(table, from, to) {
  X <- deparse(substitute(table))
  k <- which(names(table) == from)
  names(table)[k] <- to
  assign(X, table, pos = parent.frame())
  invisible(table)
}

### at test
A B C D 
1 2 3 4
A B Z D 
1 2 3 4
-- but don't do it, there's a sport!

Your solution would work if you were just able to use it in an assignment

x <- rename(x, "C", "Z")

but your function could do with a little tidying up.


Bill Venables.