Dan KS Dan KS -4 years ago 119
R Question

Using functions to change variable names from upper to lower

I'm working with a bunch of SAS datasets and I prefer the variable names to all be lowercase, using

, but I do it so often I wanted to write a function. This method works fine,

df <- data.frame(ALLIGATOR=1:4, BLUEBIRD=rnorm(4))
names(file1) <- tolower(names(file1))

but when I try to put it into a function it doesn't assign.

lower <- function (df) {names(df) <- tolower(names(df))}

I know that there is some larger concept that I'm missing, that is blocking me. It doesn't seem to do anything.

Answer Source

Arguments in R are passed by copy. You have to do:

lower <- function (df) {
  names(df) <- tolower(names(df))
file1 <- lower(file1)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download