The documentation for assign
says:
x a variable name, given as a character string.
You try to use it with something else than a variable name, which seems not to be working.
I have simplified your problem to three data frames with three columns, just to make creating the sample data easier. So these are the data frames:
df1 <- data.frame(a = 1:10, b = 1:10, c = 1:10)
df2 <- df1
df3 <- df1
And this is the loop that changes the names of the third columns:
for (i in paste0("df",1:3)){
call <- bquote(names(.(as.name(i)))[3] <- i)
eval(call)
}
What I do here first is create the call (you can look at call
to see that it contains the line of code that you actually want to evaluate). as.name(i)
converts the character i
into a variable name, and .()
tells bquote to replace known symbols inside the brackets by their value. The known symbol inside is i
which will be replaced by "df1"
and the character string is then converted to a symbol.
Once the call is constructed, it is evaluated with eval()
.