Rename objects in environment rSetting environment variables on OS XHow do I set environment variables from Java?Append an object to a list in R in amortized constant time, O(1)?How do I rename an R object?Grouping functions (tapply, by, aggregate) and the *apply familyRead environment variables in Node.jsHow to access environment variable values?List all environment variables from command line?How do I delete an exported environment variable?How do I pass environment variables to Docker containers?
Why didn't this hurt this character as badly?
Upright [...] in italics quotation
TikZ how to make supply and demand arrows for nodes?
How to back up a running remote server?
Bayes Nash Equilibria in Battle of Sexes
What's the polite way to say "I need to urinate"?
Why is current rating for multicore cable lower than single core with the same cross section?
Subtleties of choosing the sequence of tenses in Russian
Why was Germany not as successful as other Europeans in establishing overseas colonies?
Can fracking help reduce CO2?
Can my Warlock attack with their familiar and remain invisible?
gnu parallel how to use with ffmpeg
Are Boeing 737-800’s grounded?
What is the point of Germany's 299 "party seats" in the Bundestag?
Why are the 2nd/3rd singular forms of present of « potere » irregular?
How to stop co-workers from teasing me because I know Russian?
Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?
Multiple options for Pseudonyms
What was the "glowing package" Pym was expecting?
Any examples of headwear for races with animal ears?
What word means to make something obsolete?
Can someone publish a story that happened to you?
Will a top journal at least read my introduction?
Were there two appearances of Stan Lee?
Rename objects in environment r
Setting environment variables on OS XHow do I set environment variables from Java?Append an object to a list in R in amortized constant time, O(1)?How do I rename an R object?Grouping functions (tapply, by, aggregate) and the *apply familyRead environment variables in Node.jsHow to access environment variable values?List all environment variables from command line?How do I delete an exported environment variable?How do I pass environment variables to Docker containers?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'd like to rename objects in environment r. For example,
y1 <- vector('list', 3)
x1 <- matrix(0, 3, 3)
x2 <- matrix(1, 3, 3)
x3 <- matrix(2, 3, 3)
y1[[1]] <- x1
y1[[2]] <- x2
y1[[3]] <- x3
y2 <- vector('list', 3)
y2[[1]] <- x1
y2[[2]] <- x2
y2[[3]] <- x3
y <- new.env()
y$y1 <- y1
y$y2 <- y2
names(y)
names(y) <- c('a', 'b')
I expected that the name of lists inside y was a and b, that is, names(y) equals c('a', 'b'),
Obs.: I can't rename manually the variables y1 and y2, I need to change them inside the environment.
r environment-variables
add a comment |
I'd like to rename objects in environment r. For example,
y1 <- vector('list', 3)
x1 <- matrix(0, 3, 3)
x2 <- matrix(1, 3, 3)
x3 <- matrix(2, 3, 3)
y1[[1]] <- x1
y1[[2]] <- x2
y1[[3]] <- x3
y2 <- vector('list', 3)
y2[[1]] <- x1
y2[[2]] <- x2
y2[[3]] <- x3
y <- new.env()
y$y1 <- y1
y$y2 <- y2
names(y)
names(y) <- c('a', 'b')
I expected that the name of lists inside y was a and b, that is, names(y) equals c('a', 'b'),
Obs.: I can't rename manually the variables y1 and y2, I need to change them inside the environment.
r environment-variables
1
I mean, why assign them asy1andy2in the first place, if you want to rename them? Because otherwise the easiest way is to simply assign them asy$aandy$b.
– Konrad Rudolph
Mar 22 at 19:06
add a comment |
I'd like to rename objects in environment r. For example,
y1 <- vector('list', 3)
x1 <- matrix(0, 3, 3)
x2 <- matrix(1, 3, 3)
x3 <- matrix(2, 3, 3)
y1[[1]] <- x1
y1[[2]] <- x2
y1[[3]] <- x3
y2 <- vector('list', 3)
y2[[1]] <- x1
y2[[2]] <- x2
y2[[3]] <- x3
y <- new.env()
y$y1 <- y1
y$y2 <- y2
names(y)
names(y) <- c('a', 'b')
I expected that the name of lists inside y was a and b, that is, names(y) equals c('a', 'b'),
Obs.: I can't rename manually the variables y1 and y2, I need to change them inside the environment.
r environment-variables
I'd like to rename objects in environment r. For example,
y1 <- vector('list', 3)
x1 <- matrix(0, 3, 3)
x2 <- matrix(1, 3, 3)
x3 <- matrix(2, 3, 3)
y1[[1]] <- x1
y1[[2]] <- x2
y1[[3]] <- x3
y2 <- vector('list', 3)
y2[[1]] <- x1
y2[[2]] <- x2
y2[[3]] <- x3
y <- new.env()
y$y1 <- y1
y$y2 <- y2
names(y)
names(y) <- c('a', 'b')
I expected that the name of lists inside y was a and b, that is, names(y) equals c('a', 'b'),
Obs.: I can't rename manually the variables y1 and y2, I need to change them inside the environment.
r environment-variables
r environment-variables
asked Mar 22 at 18:38
Wagner JorgeWagner Jorge
84
84
1
I mean, why assign them asy1andy2in the first place, if you want to rename them? Because otherwise the easiest way is to simply assign them asy$aandy$b.
– Konrad Rudolph
Mar 22 at 19:06
add a comment |
1
I mean, why assign them asy1andy2in the first place, if you want to rename them? Because otherwise the easiest way is to simply assign them asy$aandy$b.
– Konrad Rudolph
Mar 22 at 19:06
1
1
I mean, why assign them as
y1 and y2 in the first place, if you want to rename them? Because otherwise the easiest way is to simply assign them as y$a and y$b.– Konrad Rudolph
Mar 22 at 19:06
I mean, why assign them as
y1 and y2 in the first place, if you want to rename them? Because otherwise the easiest way is to simply assign them as y$a and y$b.– Konrad Rudolph
Mar 22 at 19:06
add a comment |
2 Answers
2
active
oldest
votes
If you can’t assign them directly with the correct name, then the easiest is to replace the environment by a new one. If you absolutely need to preserve the environment (because it’s referenced elsewhere), you can replace its contents using the same trick:
objs = mget(ls(env), env)
rm(list = ls(env), envir = env)
list2env(setNames(objs, new_names), env)
The relevant part here is the last parameter to list2env: if you leave it off, this just creates a new environment. If you specify an existing environment, the names are added to that instead.
This code will leave hidden names (i.e. names starting with .) untouched — to change this, provide the all.names argument to ls, or use names.
add a comment |
R doesn't really have a built in operation to rename variables in any environment. YOu could write a simple helper function to do that.
env_rename <- function(e, new_names, old_names = names(e))
stopifnot(length(new_names)==length(old_names))
orig_val <- mget(old_names, envir=e)
rm(list=old_names, envir=e)
for(i in seq_along(old_names))
assign(new_names[i], orig_val[[i]], envir=e)
and call that with
env_rename(y, c("a","b"))
This won’t work if the old and new names overlap.
– Konrad Rudolph
Mar 22 at 19:08
@KonradRudolph Good point. I'll fix.
– MrFlick
Mar 22 at 19:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305932%2frename-objects-in-environment-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you can’t assign them directly with the correct name, then the easiest is to replace the environment by a new one. If you absolutely need to preserve the environment (because it’s referenced elsewhere), you can replace its contents using the same trick:
objs = mget(ls(env), env)
rm(list = ls(env), envir = env)
list2env(setNames(objs, new_names), env)
The relevant part here is the last parameter to list2env: if you leave it off, this just creates a new environment. If you specify an existing environment, the names are added to that instead.
This code will leave hidden names (i.e. names starting with .) untouched — to change this, provide the all.names argument to ls, or use names.
add a comment |
If you can’t assign them directly with the correct name, then the easiest is to replace the environment by a new one. If you absolutely need to preserve the environment (because it’s referenced elsewhere), you can replace its contents using the same trick:
objs = mget(ls(env), env)
rm(list = ls(env), envir = env)
list2env(setNames(objs, new_names), env)
The relevant part here is the last parameter to list2env: if you leave it off, this just creates a new environment. If you specify an existing environment, the names are added to that instead.
This code will leave hidden names (i.e. names starting with .) untouched — to change this, provide the all.names argument to ls, or use names.
add a comment |
If you can’t assign them directly with the correct name, then the easiest is to replace the environment by a new one. If you absolutely need to preserve the environment (because it’s referenced elsewhere), you can replace its contents using the same trick:
objs = mget(ls(env), env)
rm(list = ls(env), envir = env)
list2env(setNames(objs, new_names), env)
The relevant part here is the last parameter to list2env: if you leave it off, this just creates a new environment. If you specify an existing environment, the names are added to that instead.
This code will leave hidden names (i.e. names starting with .) untouched — to change this, provide the all.names argument to ls, or use names.
If you can’t assign them directly with the correct name, then the easiest is to replace the environment by a new one. If you absolutely need to preserve the environment (because it’s referenced elsewhere), you can replace its contents using the same trick:
objs = mget(ls(env), env)
rm(list = ls(env), envir = env)
list2env(setNames(objs, new_names), env)
The relevant part here is the last parameter to list2env: if you leave it off, this just creates a new environment. If you specify an existing environment, the names are added to that instead.
This code will leave hidden names (i.e. names starting with .) untouched — to change this, provide the all.names argument to ls, or use names.
answered Mar 22 at 19:10
Konrad RudolphKonrad Rudolph
405k1017961042
405k1017961042
add a comment |
add a comment |
R doesn't really have a built in operation to rename variables in any environment. YOu could write a simple helper function to do that.
env_rename <- function(e, new_names, old_names = names(e))
stopifnot(length(new_names)==length(old_names))
orig_val <- mget(old_names, envir=e)
rm(list=old_names, envir=e)
for(i in seq_along(old_names))
assign(new_names[i], orig_val[[i]], envir=e)
and call that with
env_rename(y, c("a","b"))
This won’t work if the old and new names overlap.
– Konrad Rudolph
Mar 22 at 19:08
@KonradRudolph Good point. I'll fix.
– MrFlick
Mar 22 at 19:09
add a comment |
R doesn't really have a built in operation to rename variables in any environment. YOu could write a simple helper function to do that.
env_rename <- function(e, new_names, old_names = names(e))
stopifnot(length(new_names)==length(old_names))
orig_val <- mget(old_names, envir=e)
rm(list=old_names, envir=e)
for(i in seq_along(old_names))
assign(new_names[i], orig_val[[i]], envir=e)
and call that with
env_rename(y, c("a","b"))
This won’t work if the old and new names overlap.
– Konrad Rudolph
Mar 22 at 19:08
@KonradRudolph Good point. I'll fix.
– MrFlick
Mar 22 at 19:09
add a comment |
R doesn't really have a built in operation to rename variables in any environment. YOu could write a simple helper function to do that.
env_rename <- function(e, new_names, old_names = names(e))
stopifnot(length(new_names)==length(old_names))
orig_val <- mget(old_names, envir=e)
rm(list=old_names, envir=e)
for(i in seq_along(old_names))
assign(new_names[i], orig_val[[i]], envir=e)
and call that with
env_rename(y, c("a","b"))
R doesn't really have a built in operation to rename variables in any environment. YOu could write a simple helper function to do that.
env_rename <- function(e, new_names, old_names = names(e))
stopifnot(length(new_names)==length(old_names))
orig_val <- mget(old_names, envir=e)
rm(list=old_names, envir=e)
for(i in seq_along(old_names))
assign(new_names[i], orig_val[[i]], envir=e)
and call that with
env_rename(y, c("a","b"))
edited Mar 22 at 19:10
answered Mar 22 at 19:02
MrFlickMrFlick
126k12143176
126k12143176
This won’t work if the old and new names overlap.
– Konrad Rudolph
Mar 22 at 19:08
@KonradRudolph Good point. I'll fix.
– MrFlick
Mar 22 at 19:09
add a comment |
This won’t work if the old and new names overlap.
– Konrad Rudolph
Mar 22 at 19:08
@KonradRudolph Good point. I'll fix.
– MrFlick
Mar 22 at 19:09
This won’t work if the old and new names overlap.
– Konrad Rudolph
Mar 22 at 19:08
This won’t work if the old and new names overlap.
– Konrad Rudolph
Mar 22 at 19:08
@KonradRudolph Good point. I'll fix.
– MrFlick
Mar 22 at 19:09
@KonradRudolph Good point. I'll fix.
– MrFlick
Mar 22 at 19:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305932%2frename-objects-in-environment-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I mean, why assign them as
y1andy2in the first place, if you want to rename them? Because otherwise the easiest way is to simply assign them asy$aandy$b.– Konrad Rudolph
Mar 22 at 19:06