R-helpers: Say I have a list: myvariables <- list(a=1:10,b=20) Is there a way to load the list components into the environment as variables based on the component names? i.e. by applying this theoretical function to myvariables I would have the variables a and b loaded into the environment without having to explicitly define them. --j -- Jonathan A. Greenberg, PhD Assistant Professor Global Environmental Analysis and Remote Sensing (GEARS) Laboratory Department of Geography and Geographic Information Science University of Illinois at Urbana-Champaign 607 South Mathews Avenue, MC 150 Urbana, IL 61801 Phone: 217-300-1924 http://www.geog.illinois.edu/~jgrn/ AIM: jgrn307, MSN: jgrn307@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 [[alternative HTML version deleted]]
On Fri, Feb 1, 2013 at 5:24 PM, Jonathan Greenberg <jgrn at illinois.edu> wrote:> R-helpers: > > Say I have a list: > > myvariables <- list(a=1:10,b=20) > > Is there a way to load the list components into the environment as > variables based on the component names? i.e. by applying this theoretical > function to myvariables I would have the variables a and b loaded into the > environment without having to explicitly define them.?list2env -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Hello, Something like this? myfun <- function(x, envir = .GlobalEnv){ nm <- names(x) for(i in seq_along(nm)) assign(nm[i], x[[i]], envir) } myvariables <- list(a=1:10,b=20) myfun(myvariables) a b Hope this helps, Rui Barradas Em 01-02-2013 22:24, Jonathan Greenberg escreveu:> R-helpers: > > Say I have a list: > > myvariables <- list(a=1:10,b=20) > > Is there a way to load the list components into the environment as > variables based on the component names? i.e. by applying this theoretical > function to myvariables I would have the variables a and b loaded into the > environment without having to explicitly define them. > > --j >
An often better approach is to use a function like with or within. These allow you to run a command with your list (data frame, environment, etc.) in the first position of the search list so they behave like they are in the global environment (actually better because they temporarily mask rather than overwrite other objects of the same name), but do not clutter up your global environment and/or search list. You can use the attach (and detach) functions, but this is generally not as good as with/within since the variables in your list can still be masked and if you don't clean up after yourself then the search path gets really cluttered (and therefore more masking and related problems). On Fri, Feb 1, 2013 at 3:24 PM, Jonathan Greenberg <jgrn@illinois.edu>wrote:> R-helpers: > > Say I have a list: > > myvariables <- list(a=1:10,b=20) > > Is there a way to load the list components into the environment as > variables based on the component names? i.e. by applying this theoretical > function to myvariables I would have the variables a and b loaded into the > environment without having to explicitly define them. > > --j > > -- > Jonathan A. Greenberg, PhD > Assistant Professor > Global Environmental Analysis and Remote Sensing (GEARS) Laboratory > Department of Geography and Geographic Information Science > University of Illinois at Urbana-Champaign > 607 South Mathews Avenue, MC 150 > Urbana, IL 61801 > Phone: 217-300-1924 > http://www.geog.illinois.edu/~jgrn/ > AIM: jgrn307, MSN: jgrn307@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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@gmail.com [[alternative HTML version deleted]]