I'm trying to change the name of column 2 of multiple data frames to the name of variable defining each data frame. My function works on it's own, but when I use lapply, the column is renamed "x[[i]]" instead of "df1" or "df2". Thanks in advance!
df1 = data.frame("a" = c(1,2,3), "b" = c(4,5,6))
df2 = data.frame("a" = c(1,2,3), "b" = c(4,5,6))
nameChange = function(x){
name = deparse(substitute(x))
colnames(x)[2] = name
return(x)
}
head(name(df)) # works
lapply(list(df1, df2), nameChange ) # column is named "x[[i]]"
I tried using lapply, sapply, mapply, etc. and the USE.NAMES=T/F parameter but nothing seems to do the trick.
One way how to do it with lapply is this one: