Assigning values to environments - merging them?
2
0
Entering edit mode
Helen Zhou ▴ 150
@helen-zhou-2654
Last seen 8.8 years ago
United States
Dear Sir/Madam I have two questions. 1) I am currently trying to add a lot of values to an environment. A simplistic example is: names <- as.character(1:100000) values <- data.frame(a=rep("a", 4), b=(rep("b", 4))) environment <- new.env(parent=emptyenv()) for (i in 1:100000) { assign(names[i], values, envir=environment) } I find that as I gets bigger this runs increasingly slow. Is this caused by the increasing size of the enviroment? Or by something else? And is there a way for me to avoid this? 2) Assume that I have two different environments that I would like to merge into one so all the values are shared, how can I accomplish this? For example with: e1 <- new.env(parent = emptyenv()) e2 <- new.env(parent = emptyenv()) assign("a", 3, envir=e1) assign("b", 4, envir=e2) how can I join e1 and e2 so I have for example e3 containing all the values. This seems to happen if I have: e1 <- e2 <- new.env(parent = emptyenv()) assign("a", 3, envir=e1) assign("b", 4, envir=e2) but is there a way to do it for enviroments that are already existing. Thanks you for any help you can give me. I am sorry if my questions are very simple, but I am a bit confused by the concept of environemnts and how to work with them. Yours truly. Mrs Helen Zhou
• 1.1k views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 5 hours ago
United States
Hi Helen, Helen Zhou wrote: > Dear Sir/Madam > > I have two questions. > > 1) I am currently trying to add a lot of values to an > environment. A simplistic example is: > > names <- as.character(1:100000) > values <- data.frame(a=rep("a", 4), b=(rep("b", 4))) > environment <- new.env(parent=emptyenv()) > for (i in 1:100000) { > assign(names[i], values, envir=environment) > } Ouch! You don't want to do that. See multiassign() in the Biobase package. > > I find that as I gets bigger this runs increasingly > slow. Is this caused by the increasing size of the > enviroment? Or by something else? And is there a way > for me to avoid this? > > 2) Assume that I have two different environments that > I would like to merge into one so all the values are > shared, how can I accomplish this? For example with: > > e1 <- new.env(parent = emptyenv()) > e2 <- new.env(parent = emptyenv()) > assign("a", 3, envir=e1) > assign("b", 4, envir=e2) > > how can I join e1 and e2 so I have for example e3 > containing all the values. This seems to happen if I > have: > > e1 <- e2 <- new.env(parent = emptyenv()) > assign("a", 3, envir=e1) > assign("b", 4, envir=e2) > > but is there a way to do it for enviroments that are > already existing. I don't know of a way to merge environments directly. However, you could just pull things out of each env into two lists, concatenate, and stuff into a new env. l1 <- as.list(e1) l2 <- as.list(e2) e3 <- l2e(c(l1, l2)) Best, Jim > > Thanks you for any help you can give me. I am sorry if > my questions are very simple, but I am a bit confused > by the concept of environemnts and how to work with > them. > > Yours truly. > Mrs Helen Zhou > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor -- James W. MacDonald, M.S. Biostatistician Affymetrix and cDNA Microarray Core University of Michigan Cancer Center 1500 E. Medical Center Drive 7410 CCGC Ann Arbor MI 48109 734-647-5623
ADD COMMENT
0
Entering edit mode
There is a slightly simpler way for merging two environments using l2e's second argument: e3 <- l2e(as.list(e1), e2) -Christos > -----Original Message----- > From: bioconductor-bounces at stat.math.ethz.ch > [mailto:bioconductor-bounces at stat.math.ethz.ch] On Behalf Of > James W. MacDonald > Sent: Tuesday, June 10, 2008 1:45 PM > To: Helen Zhou > Cc: bioconductor at stat.math.ethz.ch > Subject: Re: [BioC] Assigning values to environments - merging them? > > Hi Helen, > > Helen Zhou wrote: > > Dear Sir/Madam > > > > I have two questions. > > > > 1) I am currently trying to add a lot of values to an > environment. A > > simplistic example is: > > > > names <- as.character(1:100000) > > values <- data.frame(a=rep("a", 4), b=(rep("b", 4))) > > environment <- new.env(parent=emptyenv()) > > for (i in 1:100000) { > > assign(names[i], values, envir=environment) > > } > > Ouch! You don't want to do that. See multiassign() in the > Biobase package. > > > > > I find that as I gets bigger this runs increasingly slow. Is this > > caused by the increasing size of the enviroment? Or by > something else? > > And is there a way for me to avoid this? > > > > 2) Assume that I have two different environments that I > would like to > > merge into one so all the values are shared, how can I accomplish > > this? For example with: > > > > e1 <- new.env(parent = emptyenv()) > > e2 <- new.env(parent = emptyenv()) > > assign("a", 3, envir=e1) > > assign("b", 4, envir=e2) > > > > how can I join e1 and e2 so I have for example e3 > containing all the > > values. This seems to happen if I > > have: > > > > e1 <- e2 <- new.env(parent = emptyenv()) assign("a", 3, envir=e1) > > assign("b", 4, envir=e2) > > > > but is there a way to do it for enviroments that are > already existing. > > I don't know of a way to merge environments directly. > However, you could just pull things out of each env into two > lists, concatenate, and stuff into a new env. > > l1 <- as.list(e1) > l2 <- as.list(e2) > e3 <- l2e(c(l1, l2)) > > Best, > > Jim > > > > > > > Thanks you for any help you can give me. I am sorry if my questions > > are very simple, but I am a bit confused by the concept of > > environemnts and how to work with them. > > > > Yours truly. > > Mrs Helen Zhou > > > > _______________________________________________ > > Bioconductor mailing list > > Bioconductor at stat.math.ethz.ch > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > Search the archives: > > http://news.gmane.org/gmane.science.biology.informatics.conductor > > -- > James W. MacDonald, M.S. > Biostatistician > Affymetrix and cDNA Microarray Core > University of Michigan Cancer Center > 1500 E. Medical Center Drive > 7410 CCGC > Ann Arbor MI 48109 > 734-647-5623 > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > >
ADD REPLY
0
Entering edit mode
Dear Christos, James and Wolfgang Thanks you for your kind help. That was just what I needed! Yours truly Helen --- Christos Hatzis <christos.hatzis at="" nuverabio.com=""> wrote: > There is a slightly simpler way for merging two > environments using l2e's > second argument: > > e3 <- l2e(as.list(e1), e2) > > -Christos > > > -----Original Message----- > > From: bioconductor-bounces at stat.math.ethz.ch > > [mailto:bioconductor-bounces at stat.math.ethz.ch] On > Behalf Of > > James W. MacDonald > > Sent: Tuesday, June 10, 2008 1:45 PM > > To: Helen Zhou > > Cc: bioconductor at stat.math.ethz.ch > > Subject: Re: [BioC] Assigning values to > environments - merging them? > > > > Hi Helen, > > > > Helen Zhou wrote: > > > Dear Sir/Madam > > > > > > I have two questions. > > > > > > 1) I am currently trying to add a lot of values > to an > > environment. A > > > simplistic example is: > > > > > > names <- as.character(1:100000) > > > values <- data.frame(a=rep("a", 4), b=(rep("b", > 4))) > > > environment <- new.env(parent=emptyenv()) > > > for (i in 1:100000) { > > > assign(names[i], values, envir=environment) > > > } > > > > Ouch! You don't want to do that. See multiassign() > in the > > Biobase package. > > > > > > > > I find that as I gets bigger this runs > increasingly slow. Is this > > > caused by the increasing size of the enviroment? > Or by > > something else? > > > And is there a way for me to avoid this? > > > > > > 2) Assume that I have two different environments > that I > > would like to > > > merge into one so all the values are shared, how > can I accomplish > > > this? For example with: > > > > > > e1 <- new.env(parent = emptyenv()) > > > e2 <- new.env(parent = emptyenv()) > > > assign("a", 3, envir=e1) > > > assign("b", 4, envir=e2) > > > > > > how can I join e1 and e2 so I have for example > e3 > > containing all the > > > values. This seems to happen if I > > > have: > > > > > > e1 <- e2 <- new.env(parent = emptyenv()) > assign("a", 3, envir=e1) > > > assign("b", 4, envir=e2) > > > > > > but is there a way to do it for enviroments that > are > > already existing. > > > > I don't know of a way to merge environments > directly. > > However, you could just pull things out of each > env into two > > lists, concatenate, and stuff into a new env. > > > > l1 <- as.list(e1) > > l2 <- as.list(e2) > > e3 <- l2e(c(l1, l2)) > > > > Best, > > > > Jim > > > > > > > > > > > > Thanks you for any help you can give me. I am > sorry if my questions > > > are very simple, but I am a bit confused by the > concept of > > > environemnts and how to work with them. > > > > > > Yours truly. > > > Mrs Helen Zhou > > > > > > _______________________________________________ > > > Bioconductor mailing list > > > Bioconductor at stat.math.ethz.ch > > > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > > Search the archives: > > > > http://news.gmane.org/gmane.science.biology.informatics.conductor > > > > -- > > James W. MacDonald, M.S. > > Biostatistician > > Affymetrix and cDNA Microarray Core > > University of Michigan Cancer Center > > 1500 E. Medical Center Drive > > 7410 CCGC > > Ann Arbor MI 48109 > > 734-647-5623 > > > > _______________________________________________ > > Bioconductor mailing list > > Bioconductor at stat.math.ethz.ch > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > Search the archives: > > > http://news.gmane.org/gmane.science.biology.informatics.conductor > > > > > > >
ADD REPLY
0
Entering edit mode
@wolfgang-huber-3550
Last seen 18 days ago
EMBL European Molecular Biology Laborat…
Dear Helen, for your first question, try with setting "hash=TRUE" in your call to "new.env". See below (absolute timings are of course machine dependent). e1=new.env() e2=new.env(hash=TRUE) nm=paste(1:25000) print( system.time( for(v in nm) assign(v, pi, envir=e1) ) ) user system elapsed 3.004 0.008 3.025 print( system.time( for(v in nm) assign(v, pi, envir=e2) ) ) user system elapsed 0.152 0.000 0.151 ---------------------------------- Re your second question: a) you could have a look at the function "mget", and at "multiassign" in Biobase. The first is written in C and promises to be fast, the second contains a loop written in R, but may still be fast enough. b) you could set the parent environment of one of your environments to be the other; when you look up a name in the former, and it is not found there, the value found in the parent is returned. a=new.env() b=new.env() parent.env(b) = a assign("q", 1, env=a) get("q", a) [1] 1 get("q", b) [1] 1 c) Note that your example code probably does not do what you expect: e1 <- e2 <- new.env(parent = emptyenv()) > e1 <environment: 0x27813e0=""> > e2 <environment: 0x27813e0=""> They point to the *same* workspace in memory. If you want to learn more about environments (and many other useful things), I recommend this book http://www.amazon.co.uk/Programming-Bioinformatics-Chapman-Computer- Analysis/dp/1420063677 Best wishes Wolfgang ------------------------------------------------------------------ Wolfgang Huber EBI/EMBL Cambridge UK http://www.ebi.ac.uk/huber Helen Zhou a ?crit 10/06/2008 17:36: > Dear Sir/Madam > > I have two questions. > > 1) I am currently trying to add a lot of values to an > environment. A simplistic example is: > > names <- as.character(1:100000) > values <- data.frame(a=rep("a", 4), b=(rep("b", 4))) > environment <- new.env(parent=emptyenv()) > for (i in 1:100000) { > assign(names[i], values, envir=environment) > } > > I find that as I gets bigger this runs increasingly > slow. Is this caused by the increasing size of the > enviroment? Or by something else? And is there a way > for me to avoid this? > > 2) Assume that I have two different environments that > I would like to merge into one so all the values are > shared, how can I accomplish this? For example with: > > e1 <- new.env(parent = emptyenv()) > e2 <- new.env(parent = emptyenv()) > assign("a", 3, envir=e1) > assign("b", 4, envir=e2) > > how can I join e1 and e2 so I have for example e3 > containing all the values. This seems to happen if I > have: > > e1 <- e2 <- new.env(parent = emptyenv()) > assign("a", 3, envir=e1) > assign("b", 4, envir=e2) > > but is there a way to do it for enviroments that are > already existing. > > Thanks you for any help you can give me. I am sorry if > my questions are very simple, but I am a bit confused > by the concept of environemnts and how to work with > them. > > Yours truly. > Mrs Helen Zhou > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
ADD COMMENT

Login before adding your answer.

Traffic: 787 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6