This is a simple question but the answer is apparently not so simple... Is it possible to combine environments in R?
E1 = new.env()
E2 = new.env()
E1$x = 25
E2$y = 7
Ok, now I want an environment (say, E3
) that has both x
and y
defined.
c(E1, E2)
#doesn't work
E3 = new.env(E1, E2)
#doesn't work
I have found other similar questions but they don't seem to work for me.
Use case: Maybe there's a reason this isn't easy...the reason I want to do this is thus: I use some functions to load up data. Previously, I've just loaded it into the global environment, but I now have many different functions loading different types of data (which I call variously as needed), and so I wanted to keep the loaded data a bit more compartmentalized. If I call 2 different loading functions E1=loadData1()
and E2=loadData2()
, and I now want to call a function that uses variables from both of these functions, I'd like to be able to say with(E1 & E2, someFunction())
. Hence, merging my loaded environments seems appropriate.
So, what's the right way to merge them? And, as an aside, do you have a different suggestion for how to better accomplish what I'm doing, if merging environments is not the right way?
One base R way to do this is with the merge() function, using the basic syntax merge(df1, df2) . The order of data frame 1 and data frame 2 doesn't matter, but whichever one is first is considered x and the second one is y.
You can merge columns, by adding new variables; or you can merge rows, by adding observations. To add columns use the function merge() which requires that datasets you will merge to have a common variable. In case that datasets doesn't have a common variable use the function cbind .
How do I concatenate two columns in R? To concatenate two columns you can use the <code>paste()</code> function. For example, if you want to combine the two columns A and B in the dataframe df you can use the following code: <code>df['AB'] <- paste(df$A, df$B)</code>.
1) Make one environment the parent of the other and use with(child, ...)
:
parent <- new.env(); parent$x <- 1
child <- new.env(parent = parent); child$y <- 2
with(child, x + y) # x comes from child and y from parent
## [1] 3
You can link as many environments as you like in as long a chain as necessary.
Note that if the child were initially created with no parent then you can add a parent later using:
parent.env(child) <- parent
Thus we define LoadData1
and LoadData2
as:
# define LoadData1 to have a parent argument
LoadData1 <- function(parent = emptyenv()) {
# calculation of environment e goes here
parent.env(e) <- parent
e
}
# define LoadData2 to have a parent argument
LoadData2 <- function(parent = emptyenv()) {
# calculation of environment e goes here
parent.env(e) <- parent
e
}
# run
e1 <- LoadData1()
e2 <- LoadData2(parent = e1)
with(e2, dataFrom1 + dataFrom2)
If you don't want to modify LoadData1
and LoadData2
from what they are now:
e1 <- LoadData1()
e2 <- LoadData2()
parent.env(e2) <- e1
with(e2, dataFrom1 + dataFrom2)
2) Convert to lists:
with(c(as.list(e1), as.list(e2)), somefunction())
ADDED Second approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With