I want to assign and store a new value for each iteration in a for loopHow to reference incrementing variables in R?Iterate all files in a directory using a 'for' loopIs there a way to access an iteration-counter in Java's for-each loop?How to determine the first and last iteration in a foreach loop?A 'for' loop to iterate over an enum in JavaHow to break out of jQuery each LoopIn Ruby, how do I skip a loop in a .each loop, similar to 'continue'Error looping through list: “Error in `[<-.data.frame`(`*tmp*`, , i, value = c(7L, 1L, 4L, 7L, 7L, : new columns would leave holes… ”Extracting Coefficient Values from Several Regressions and Storing in New Matrixigraph loop over intercluster analysis of communitiesRun a pop-gen simulation several times and store the results of each in a new column on a data frame

What is the bio-mechanical plausibility of a fox with venomous fangs?

What is the German idiom or expression for when someone is being hypocritical against their own teachings?

What are these circular spots on these Ariane V SRB nozzles?

Is the Folding Boat truly seaworthy?

How to help new students accept function notation

Getting an entry level IT position later in life

Is it a bad idea to offer variants of a final exam based on the type of allowed calculators?

"In charge of" vs "Responsible for"

Did Apollo leave poop on the moon?

Responding to Plague Engineer

How do these cubesats' whip antennas work?

Is space radiation a risk for space film photography, and how is this prevented?

What city skyline is this picture of?

Does a 4 bladed prop have almost twice the thrust of a 2 bladed prop?

Why do private jets such as Gulfstream fly higher than other civilian jets?

How to halve redstone signal strength?

The heat content of the products is more than that of the reactant in an ............. reaction

How to check a file was encrypted (really & correctly)

Based on what criteria do you add/not add icons to labels within a toolbar?

Is Odin inconsistent about the powers of Mjolnir?

The size of sheafification

Did silent film actors actually say their lines or did they simply improvise “dialogue” while being filmed?

12V lead acid charger with LM317 not charging

Why do proponents of guns oppose gun competency tests?



I want to assign and store a new value for each iteration in a for loop


How to reference incrementing variables in R?Iterate all files in a directory using a 'for' loopIs there a way to access an iteration-counter in Java's for-each loop?How to determine the first and last iteration in a foreach loop?A 'for' loop to iterate over an enum in JavaHow to break out of jQuery each LoopIn Ruby, how do I skip a loop in a .each loop, similar to 'continue'Error looping through list: “Error in `[<-.data.frame`(`*tmp*`, , i, value = c(7L, 1L, 4L, 7L, 7L, : new columns would leave holes… ”Extracting Coefficient Values from Several Regressions and Storing in New Matrixigraph loop over intercluster analysis of communitiesRun a pop-gen simulation several times and store the results of each in a new column on a data frame






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to assign and store a new value into the R environment for each iteration of a for loop as said in the title.



csg<-clusters(suba)

csgs1<-subgraph(suba, csg$membership==1)

sg1<-cluster_spinglass(csgs, weights=E(a)$weight)


So, given some igraph object (suba), I want to create sg1, sg2,..., sg31. I don't want to go through the 2 lines of code above 31 times though so I've tried a for loop? But I don't know how to store a new value each time.



for (k in 1:seq_along(length(csg$csize)))

csgs[k] <- subgraph(suba, csg$membership==k)

sg [k] <-cluster_spinglass(csgs[k] , weights=E(a)$weight)




Obviously, the square brackets don't work in this situation but I couldn't find how to do this.



Ideally, I'd want to have sg1,sg2, etc saved into the R environment so that I could use these as the groups for my network suba. Because my code is straight up not working, I don't get results at the moment. It works individually though, creating a subgraph for csgs1 and a list for sg1.










share|improve this question


























  • See my "store as list elements" answer here for one simple approach

    – Dan Y
    Mar 27 at 5:04











  • Also, you should initialize a loop with for(k in 1:length(vec)) or even better with for(k in seq_along(vec)). Right now, your code does 1:seq_along(length(vec)) which is effectively 1:1 and -- I suspect -- not what is intended.

    – Dan Y
    Mar 27 at 5:48

















0















I want to assign and store a new value into the R environment for each iteration of a for loop as said in the title.



csg<-clusters(suba)

csgs1<-subgraph(suba, csg$membership==1)

sg1<-cluster_spinglass(csgs, weights=E(a)$weight)


So, given some igraph object (suba), I want to create sg1, sg2,..., sg31. I don't want to go through the 2 lines of code above 31 times though so I've tried a for loop? But I don't know how to store a new value each time.



for (k in 1:seq_along(length(csg$csize)))

csgs[k] <- subgraph(suba, csg$membership==k)

sg [k] <-cluster_spinglass(csgs[k] , weights=E(a)$weight)




Obviously, the square brackets don't work in this situation but I couldn't find how to do this.



Ideally, I'd want to have sg1,sg2, etc saved into the R environment so that I could use these as the groups for my network suba. Because my code is straight up not working, I don't get results at the moment. It works individually though, creating a subgraph for csgs1 and a list for sg1.










share|improve this question


























  • See my "store as list elements" answer here for one simple approach

    – Dan Y
    Mar 27 at 5:04











  • Also, you should initialize a loop with for(k in 1:length(vec)) or even better with for(k in seq_along(vec)). Right now, your code does 1:seq_along(length(vec)) which is effectively 1:1 and -- I suspect -- not what is intended.

    – Dan Y
    Mar 27 at 5:48













0












0








0








I want to assign and store a new value into the R environment for each iteration of a for loop as said in the title.



csg<-clusters(suba)

csgs1<-subgraph(suba, csg$membership==1)

sg1<-cluster_spinglass(csgs, weights=E(a)$weight)


So, given some igraph object (suba), I want to create sg1, sg2,..., sg31. I don't want to go through the 2 lines of code above 31 times though so I've tried a for loop? But I don't know how to store a new value each time.



for (k in 1:seq_along(length(csg$csize)))

csgs[k] <- subgraph(suba, csg$membership==k)

sg [k] <-cluster_spinglass(csgs[k] , weights=E(a)$weight)




Obviously, the square brackets don't work in this situation but I couldn't find how to do this.



Ideally, I'd want to have sg1,sg2, etc saved into the R environment so that I could use these as the groups for my network suba. Because my code is straight up not working, I don't get results at the moment. It works individually though, creating a subgraph for csgs1 and a list for sg1.










share|improve this question
















I want to assign and store a new value into the R environment for each iteration of a for loop as said in the title.



csg<-clusters(suba)

csgs1<-subgraph(suba, csg$membership==1)

sg1<-cluster_spinglass(csgs, weights=E(a)$weight)


So, given some igraph object (suba), I want to create sg1, sg2,..., sg31. I don't want to go through the 2 lines of code above 31 times though so I've tried a for loop? But I don't know how to store a new value each time.



for (k in 1:seq_along(length(csg$csize)))

csgs[k] <- subgraph(suba, csg$membership==k)

sg [k] <-cluster_spinglass(csgs[k] , weights=E(a)$weight)




Obviously, the square brackets don't work in this situation but I couldn't find how to do this.



Ideally, I'd want to have sg1,sg2, etc saved into the R environment so that I could use these as the groups for my network suba. Because my code is straight up not working, I don't get results at the moment. It works individually though, creating a subgraph for csgs1 and a list for sg1.







r loops for-loop networking






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 8:59









Curr195

1,3223 gold badges16 silver badges35 bronze badges




1,3223 gold badges16 silver badges35 bronze badges










asked Mar 27 at 4:59









badatrbadatr

31 bronze badge




31 bronze badge















  • See my "store as list elements" answer here for one simple approach

    – Dan Y
    Mar 27 at 5:04











  • Also, you should initialize a loop with for(k in 1:length(vec)) or even better with for(k in seq_along(vec)). Right now, your code does 1:seq_along(length(vec)) which is effectively 1:1 and -- I suspect -- not what is intended.

    – Dan Y
    Mar 27 at 5:48

















  • See my "store as list elements" answer here for one simple approach

    – Dan Y
    Mar 27 at 5:04











  • Also, you should initialize a loop with for(k in 1:length(vec)) or even better with for(k in seq_along(vec)). Right now, your code does 1:seq_along(length(vec)) which is effectively 1:1 and -- I suspect -- not what is intended.

    – Dan Y
    Mar 27 at 5:48
















See my "store as list elements" answer here for one simple approach

– Dan Y
Mar 27 at 5:04





See my "store as list elements" answer here for one simple approach

– Dan Y
Mar 27 at 5:04













Also, you should initialize a loop with for(k in 1:length(vec)) or even better with for(k in seq_along(vec)). Right now, your code does 1:seq_along(length(vec)) which is effectively 1:1 and -- I suspect -- not what is intended.

– Dan Y
Mar 27 at 5:48





Also, you should initialize a loop with for(k in 1:length(vec)) or even better with for(k in seq_along(vec)). Right now, your code does 1:seq_along(length(vec)) which is effectively 1:1 and -- I suspect -- not what is intended.

– Dan Y
Mar 27 at 5:48












2 Answers
2






active

oldest

votes


















1














If you want to really have multiple variables created, then you can do it as follows:



for (k in seq_along(csg$csize))
assign(paste0("csgs",k), subgraph(suba, csg$membership==1))
assign(paste0("sg",k), cluster_spinglass(get(paste0("csgs",k)), weights=E(a)$weight))



This will create you csgs1, csgs2, csgs3, etc. and sg1, sg2, sg3, etc., so that you can use them directly later in your code.



Hope it helps.






share|improve this answer



























  • Thanks for that, I was pretty much just looking for the paste0 thing. :)

    – badatr
    Mar 27 at 5:54











  • I have updated the answer to achieve what you exactly want. Glad I could help.

    – TeeKea
    Mar 27 at 5:59


















0














It is better if you store things as list instead of flooding your environment with multiple objects.



However, you could use assign to create a new object and assign a value like:



assign("a", 10) #Create an object "a" and assign value 10
a
[1] 10





share|improve this answer



























    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%2f55370086%2fi-want-to-assign-and-store-a-new-value-for-each-iteration-in-a-for-loop%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 want to really have multiple variables created, then you can do it as follows:



    for (k in seq_along(csg$csize))
    assign(paste0("csgs",k), subgraph(suba, csg$membership==1))
    assign(paste0("sg",k), cluster_spinglass(get(paste0("csgs",k)), weights=E(a)$weight))



    This will create you csgs1, csgs2, csgs3, etc. and sg1, sg2, sg3, etc., so that you can use them directly later in your code.



    Hope it helps.






    share|improve this answer



























    • Thanks for that, I was pretty much just looking for the paste0 thing. :)

      – badatr
      Mar 27 at 5:54











    • I have updated the answer to achieve what you exactly want. Glad I could help.

      – TeeKea
      Mar 27 at 5:59















    1














    If you want to really have multiple variables created, then you can do it as follows:



    for (k in seq_along(csg$csize))
    assign(paste0("csgs",k), subgraph(suba, csg$membership==1))
    assign(paste0("sg",k), cluster_spinglass(get(paste0("csgs",k)), weights=E(a)$weight))



    This will create you csgs1, csgs2, csgs3, etc. and sg1, sg2, sg3, etc., so that you can use them directly later in your code.



    Hope it helps.






    share|improve this answer



























    • Thanks for that, I was pretty much just looking for the paste0 thing. :)

      – badatr
      Mar 27 at 5:54











    • I have updated the answer to achieve what you exactly want. Glad I could help.

      – TeeKea
      Mar 27 at 5:59













    1












    1








    1







    If you want to really have multiple variables created, then you can do it as follows:



    for (k in seq_along(csg$csize))
    assign(paste0("csgs",k), subgraph(suba, csg$membership==1))
    assign(paste0("sg",k), cluster_spinglass(get(paste0("csgs",k)), weights=E(a)$weight))



    This will create you csgs1, csgs2, csgs3, etc. and sg1, sg2, sg3, etc., so that you can use them directly later in your code.



    Hope it helps.






    share|improve this answer















    If you want to really have multiple variables created, then you can do it as follows:



    for (k in seq_along(csg$csize))
    assign(paste0("csgs",k), subgraph(suba, csg$membership==1))
    assign(paste0("sg",k), cluster_spinglass(get(paste0("csgs",k)), weights=E(a)$weight))



    This will create you csgs1, csgs2, csgs3, etc. and sg1, sg2, sg3, etc., so that you can use them directly later in your code.



    Hope it helps.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 27 at 5:58

























    answered Mar 27 at 5:26









    TeeKeaTeeKea

    3,7115 gold badges19 silver badges33 bronze badges




    3,7115 gold badges19 silver badges33 bronze badges















    • Thanks for that, I was pretty much just looking for the paste0 thing. :)

      – badatr
      Mar 27 at 5:54











    • I have updated the answer to achieve what you exactly want. Glad I could help.

      – TeeKea
      Mar 27 at 5:59

















    • Thanks for that, I was pretty much just looking for the paste0 thing. :)

      – badatr
      Mar 27 at 5:54











    • I have updated the answer to achieve what you exactly want. Glad I could help.

      – TeeKea
      Mar 27 at 5:59
















    Thanks for that, I was pretty much just looking for the paste0 thing. :)

    – badatr
    Mar 27 at 5:54





    Thanks for that, I was pretty much just looking for the paste0 thing. :)

    – badatr
    Mar 27 at 5:54













    I have updated the answer to achieve what you exactly want. Glad I could help.

    – TeeKea
    Mar 27 at 5:59





    I have updated the answer to achieve what you exactly want. Glad I could help.

    – TeeKea
    Mar 27 at 5:59













    0














    It is better if you store things as list instead of flooding your environment with multiple objects.



    However, you could use assign to create a new object and assign a value like:



    assign("a", 10) #Create an object "a" and assign value 10
    a
    [1] 10





    share|improve this answer





























      0














      It is better if you store things as list instead of flooding your environment with multiple objects.



      However, you could use assign to create a new object and assign a value like:



      assign("a", 10) #Create an object "a" and assign value 10
      a
      [1] 10





      share|improve this answer



























        0












        0








        0







        It is better if you store things as list instead of flooding your environment with multiple objects.



        However, you could use assign to create a new object and assign a value like:



        assign("a", 10) #Create an object "a" and assign value 10
        a
        [1] 10





        share|improve this answer













        It is better if you store things as list instead of flooding your environment with multiple objects.



        However, you could use assign to create a new object and assign a value like:



        assign("a", 10) #Create an object "a" and assign value 10
        a
        [1] 10






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 5:09









        SonnySonny

        2,7051 gold badge6 silver badges17 bronze badges




        2,7051 gold badge6 silver badges17 bronze badges






























            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%2f55370086%2fi-want-to-assign-and-store-a-new-value-for-each-iteration-in-a-for-loop%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            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

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현