'Making multiple assignments to objects named as strings

We can assign a value to a single object using that object's name - assign("x", 1) - and we can efficiently assign different values to multiple object thanks to the zeallot package - c(x, y) %<-% c(1, 2) - but can we do both? I basically just want to do c("x", "y") %<-% c(1, 2) and I can only think to do it in this lovely way:

invisible(mapply(function(i, j) assign(i, j, envir = .GlobalEnv), i = c("x", "y"), j = c(1, 2)))

Is there any better way?



Solution 1:[1]

You can list named elements of a list to an environment:

list2env(setNames(as.list(c(1,2)),c("x","y")),.GlobalEnv)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 onyambu