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;








1















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.










share|improve this question

















  • 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

















1















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.










share|improve this question

















  • 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













1












1








1








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 18:38









Wagner JorgeWagner Jorge

84




84







  • 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












  • 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







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












2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer






























    0














    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"))





    share|improve this answer

























    • 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











    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer



























      1














      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.






      share|improve this answer

























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 19:10









        Konrad RudolphKonrad Rudolph

        405k1017961042




        405k1017961042























            0














            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"))





            share|improve this answer

























            • 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















            0














            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"))





            share|improve this answer

























            • 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













            0












            0








            0







            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"))





            share|improve this answer















            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"))






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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

















            • 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

















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해