I am trying to rename columns of multiple data.frames.
To give an example, let's say I've a list of data.frames dfA, dfB and dfC. I wrote a function changeNames to set names accordingly and then used lapply as follows:
dfs <- list(dfA, dfB, dfC)
ChangeNames <- function(x) {
names(x) <- c("A", "B", "C" )
}
lapply(dfs, ChangeNames)
However, this doesn't work as expected. It seems that I am not assigning the new names to the data.frame, rather only creating the new names. What am I doing wrong here?
Thank you in advance!
names(x) <-in your function, addreturn(x)or simplyx. Else, you're returning justnames(x). - Arunlapplydoes not modify the input. There's no "change by reference" happening here. Everything is being done on a copy. You'll have to assign the result back. do:dfs <- lapply(dfs, ChangeNames)- ArundfA <- dfs[[1]]... ? - Arun