In R, are the following sets of expressions functionally similar to one another?
assign(x,y)
, andeval(parse(text = "x"))
andx=as.name("string")
get(x)
, anddeparse(substitute(x))
and____
(x=as.string(object)
)?do.call("<-",list(x, y))
For example, can each of them be used in the following batch process?
## example data tables (requires data.table package I believe)
(dt1 <- data.table(urlteststatus = letters[1:24], urltest = letters[1:24], istest = letters[23:24] ) )
(dt2 <- data.table(istest = letters[2:2], urlteststatus = letters[1:10], urltest = paste0(letters[3:3], letters[1:10]) ) )
# list of variables to iterate over
tlist <- list("dt1","dt2")
# Batch process: This creates a new variable "x.2" which concatenates the the variables "urltest", "istest", and "urlteststatus".
for (x in tlist) {
assign(paste0(x,".2"), mutate(assign(x, get(x)),
r_teststatus = paste(urltest,
istest,
urlteststatus)))
}
These examples are from: Convert string to a variable name, Adding data frames as list elements (using for loop), and How to convert string to a dataframe name pandas/python.
PS: Frequently in the response, I see comments like "that's not a good way to do it, a more R-like way would be to. . ." but they usually don't have a link to what the more R-like way would be. Please explain or link to what the "right" way is, and why.