setq and defvar in Lisp

There are several ways to introduce variables. DEFVAR and DEFPARAMETER introduce global dynamic variables. DEFVAR optionally sets it to some value, unless it is already defined. DEFPARAMETER sets it always to the provided value. SETQ does not introduce a variable. (defparameter *number-of-processes* 10) (defvar *world* (make-world)) ; the world is made only once. Notice that … Read more

Assign multiple objects to .GlobalEnv from within a function

Update of 2018-10-10: The most succinct way to carry out this specific task is to use list2env() like so: ## Create an example list of five data.frames df <- data.frame(x = rnorm(25), g = rep(factor(LETTERS[1:5]), 5)) LIST <- split(df, df$g) ## Assign them to the global environment list2env(LIST, envir = .GlobalEnv) ## Check that it … Read more

Assign multiple new variables on LHS in a single line

There is a great answer on the Struggling Through Problems Blog This is taken from there, with very minor modifications. USING THE FOLLOWING THREE FUNCTIONS (Plus one for allowing for lists of different sizes) # Generic form ‘%=%’ = function(l, r, …) UseMethod(‘%=%’) # Binary Operator ‘%=%.lbunch’ = function(l, r, …) { Envir = as.environment(-1) … Read more

How to assign from a function which returns more than one value?

(1) list[…]<- I had posted this over a decade ago on r-help. Since then it has been added to the gsubfn package. It does not require a special operator but does require that the left hand side be written using list[…] like this: library(gsubfn) # need 0.7-0 or later list[a, b] <- functionReturningTwoValues() If you … Read more

Why is using assign bad?

Actually those two operations are quite different. The first gives you 26 different objects while the second gives you only one. The second object will be a lot easier to use in analyses. So I guess I would say you have already demonstrated the major downside of assign, namely the necessity of then needing always … Read more