[R] Using assign with mapply

Julio Sergio Santana juliosergio at gmail.com
Fri Dec 6 20:27:10 CET 2013


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.



More information about the R-help mailing list