I have a data frame whose first colum contains the names of the variables and whose second colum contains the values to assign to them: : kkk <- data.frame(vars=c("var1", "var2", "var3"), vals=c(10, 20, 30), stringsAsFactors=F) If I do : assign(kkk$vars[1], kkk$vals[1]) it works : var1 [1] 10 However, if I try with mapply this is what I get: : mapply(assign, kkk$vars, kkk$vals) var1 var2 var3 10 20 30 : var2 Error: object 'var2' not found Maybe I have not undestand how mapply and assign work. Do you have any comments? Thanks, -Sergio.
On Dec 6, 2013, at 11:27 AM, Julio Sergio Santana wrote:> I have a data frame whose first colum contains the names of the variables > and whose second colum contains the values to assign to them: > > : kkk <- data.frame(vars=c("var1", "var2", "var3"), > vals=c(10, 20, 30), stringsAsFactors=F) > > If I do > > : assign(kkk$vars[1], kkk$vals[1]) > > it works > > : var1 > [1] 10 > > However, if I try with mapply this is what I get: > > : mapply(assign, kkk$vars, kkk$vals) > var1 var2 var3 > 10 20 30 > : var2 > Error: object 'var2' not found > > Maybe I have not undestand how mapply and assign work. Do you have > any comments?I think you will find that the value returned from the mapply call was a three element list with the desired names and values ... except you then gave that enclosing list no name and it will be garbage-collected. If you want to have 'assign' do its magic into the global environment, then you need to supply 'mapply' a MoreArgs argument on the other side of the ellipsis: Usage: mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE) So what happens if you try this: mapply(assign, kkk$vars, kkk$vals, MoreArgs = list(envir = .GlobalEnv) -- David Winsemius Alameda, CA, USA
Julio Sergio Santana <juliosergio <at> gmail.com> writes:> > I have a data frame whose first colum contains the names of the variables > and whose second colum contains the values to assign to them: > > : kkk <- data.frame(vars=c("var1", "var2", "var3"), > vals=c(10, 20, 30), stringsAsFactors=F) >For those interested in the problem this is how I solved the problem: I want to have something similar to: # # var1 <- 10 # var2 <- 20 # var3 <- 30 my first trial was: mapply(assign, kkk$vars, kkk$vals) ## var1 var2 var3 ## 10 20 30 # This is, however, what I got: var1 ## Error: object 'var1' not found David Winsemius suggested me something similar to mapply(assign, kkk$vars, kkk$vals, MoreArgs = list(pos = 1)) # or: mapply(assign, kkk$vars, kkk$vals, MoreArgs = list(envir = .GlobalEnv)) var1 ## [1] 10 This almost works, but what if this construction is used inside a function? example <- function () { var1 <- 250 kkk <- data.frame(vars=c("var1", "var2", "var3"), vals=c(10, 20, 30), stringsAsFactors=F) mapply(assign, kkk$vars, kkk$vals, MoreArgs = list(pos = 1)) print (var2) print (var1) } example() ## [1] 20 ## [1] 250 var1, which was defined inside the function, isn't modified To fix this, I defined the function as follows: example <- function () { var1 <- 250 kkk <- data.frame(vars=c("var1", "var2", "var3"), vals=c(10, 20, 30), stringsAsFactors=F) mapply(assign, kkk$vars, kkk$vals, MoreArgs = list(pos = sys.frame(sys.nframe()))) # sys.nframe() is the number of the frame created inside the function # and sys.frame() establishes it as the one assign uses to set values print (var2) print (var1) } example() ## [1] 20 ## [1] 10 And the purpose is got Thanks, -Sergio.