Questions like R variable names in loop, get, etc are common among people coming to R from other languages. The standard answer is usually, as I gave in that example, that it's not possible to iterate through a list of variables in the global environment and modify the underlying variable, only a copy. This fits in with the R semantics of not passing values by reference and creating copies of objects as necessary.
However, this type of construct works:
xData <- data.frame(a = 1:2, b = 3:4)
yData <- data.frame(a = 4:5, b = 6:7)
varList <- ls(pattern = "Data$")
for(var in varList) {
.GlobalEnv[[var]]$c <- with(.GlobalEnv[[var]], a + b)
}
xData
# a b c
# 1 1 3 4
# 2 2 4 6
Other than being poor programming style, and not really fitting with the 'R' way of doing things, are there any specific issues with this style of coding?
Please note I'm not advocating this as a good practice, just curious as to whether this is likely to have unintended side effects.