Load model parameters from a data.frame into R environment

Viewed 31

Example: I have the following parameter table:

df <- data.frame(vn=letters[1:3],values=1:3)

I want an automatic way to realize the following R code:

a <- 1
b <- 2
c <- 3

I know I can do the following to brute force it:

invisible(lapply(1:nrow(df), function(x) assign(df[x,1], df[x,2], envir=.GlobalEnv)))

But I am looking for a more beautiful way to do it.

1 Answers

Use mapply when you want to iterate over more than 1 list/vector in parallel:

with(df, mapply(assign, x = vn, value = values, MoreArgs = list(envir = .GlobalEnv)))
Related