[R] Using assign with mapply
Greg Snow
538280 at gmail.com
Wed Dec 18 21:07:10 CET 2013
The take home message that you should be learning from your struggles
is to "Not Use The 'assign' Function!" and "Do Not Use Global
Variables Like This".
R has lists (and environments) that make working with objects that are
associated with each other much simpler and fits better with the
functional programming style of R.
For example you can create a list from your data frame quickly and
easily with code like:
mydata <- as.list(kkk$vals)
names(mydata) <- kkk$vars
or
mydata <- setNames( as.list(kkk$vals), kkk$vars )
then you will have you variables (with names) inside the list (mydata
in this example, but name it whatever you want)
This list can then be passed out of a function or otherwise used.
To access a specific variable by name you can do:
mydata$var1
or
mydata[['var2']]
or
with(mydata, var3)
but you can also do things like (and this is often a follow-up
question to questions like yours):
varname <- 'var3'
mydata[[ varname ]]
and you can also use lapply and sapply to do the same action on every
variable in your list:
sapply( mydata, function(x) x + 5 )
instead of having to loop through a bunch of global variables.
And if you want to save or delete these, now you just save or delete
the entire list rather than having to loop through the set of global
variables.
If you tell us more about how you want to use these variables we can
give more suggestions, but the main point is that in the long run you
will be happier learning to use lists (and possibly environments) in
place of trying to create and work with global variables like you
asked about.
On Mon, Dec 16, 2013 at 8:55 AM, Julio Sergio Santana
<juliosergio at gmail.com> wrote:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com
More information about the R-help
mailing list