suggests that we have a valid data.frame with 50 rows in each
dat2 <- data.frame(c1 = 1:50, VAR1 = 51:100)
1. Do not use assign and get if you can avoid this.
"dat2[,"VAR1"]" not valid in R
You can also mark this from the help page for assign
assign does not send assignment methods, so it cannot be used to set vector elements, names, attributes, etc.
Note that assigning a linked list or data frame changes the attached copy, not the original object: see attachment and p.
The data.frame column is a list item.
What you are looking for is [[<-
If you want to assign new values ββto VAR1 inside dat2 ,
dat2[['VAR1']] <- 1:50
The answer to your question....
To fully manipulate character strings with get and assign
assign('dat2', `[[<-`(get('dat2'), 'VAR1', value = 2:51))
Other approaches
data.table :: set
if you want to assign by reference in data.frame or data.table (instead of the existing column only), then set from the data.table package works (even with data.frames )
library(data.table) set(dat2, j = 'VAR1', value = 5:54)
eval and bquote
dat1 <- data.frame(x=1:5) dat2 <- data.frame(x=2:6) for(x in sapply(c('dat1','dat2'),as.name)) { eval(bquote(.(x)[['VAR1']] <- 2:6)) }
eapply
Or if you are using a separate environment
ee <- new.env() ee$dat1 <- dat1 ee$dat2 <- dat2