[R] Using assign with mapply

Greg Snow 538280 at gmail.com
Fri Dec 20 17:03:48 CET 2013


Trying to understand environments is not for the faint of heart.  If
lists do what you want then I would stick with a list and not worry
about the environments.  Most of the time that you deal with
environments everything happens automatically behind the scenes and
you don't need to worry about the details.

If you want to learn more about environments there here is a start.
Many of the ways of working with environments is the same as working
with lists (you can access and assign with `[[` etc.) and there are
`as.` functions to convert between them.  The biggest difference is
that environments use references instead of copies (powerful but
dangerous).  If you do something like

mylist2 <- mylist
mylist2$x <- 1

then mylist 2 will be a copy of mylist 1 and the value of x within
mylist2 will be created or modified (but mylist will remain
unchanged).  However if you do the same thing with an environment then
a copy is not made and the `x` variable in the original environment
will be created or changed.  This can be a big benefit when you have a
large data object that needs to be passed to multiple functions, with
an environment the data will never be copied, with a list or other
object you may end up with multiple copies (though R is really good at
not making copies when it does not need to, but sometimes you still
end up with more copies than needed when R cannot tell if a copy is
needed or not), but this is dangerous in that if you make any changes
then the original is changed as well (the regular mechanism of making
copies on changes protects the original).

Environments also use hashing for name look ups which can speed things
up when you have a large number of variables that you are accessing by
name (but most usual cases are quick enough that you will not notice
when using lists or other objects).

Hope that helps,

On Thu, Dec 19, 2013 at 10:17 AM, Julio Sergio Santana
<juliosergio at gmail.com> wrote:
> Greg Snow <538280 <at> gmail.com> writes:
>
>>
>> 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.
>>
>
> Thanks, Greg!
>
> Yours is a very smart solution to the problem I posed.
>
> By the way, what I'm trying to do is reading from a file a set of user given
> parameters, in two paired columns: parameter-name, value; and then, managing
> these parameters inside my R program. Now I do understand a bit more about
> lists. What about environments? Are they similar to lists, and when, and how
> are they created?
>
> Best regrards,
>
>   -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