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;
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
add a comment |
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
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 withfor(k in 1:length(vec))
or even better withfor(k in seq_along(vec))
. Right now, your code does1:seq_along(length(vec))
which is effectively1:1
and -- I suspect -- not what is intended.
– Dan Y
Mar 27 at 5:48
add a comment |
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
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
r loops for-loop networking
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 withfor(k in 1:length(vec))
or even better withfor(k in seq_along(vec))
. Right now, your code does1:seq_along(length(vec))
which is effectively1:1
and -- I suspect -- not what is intended.
– Dan Y
Mar 27 at 5:48
add a comment |
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 withfor(k in 1:length(vec))
or even better withfor(k in seq_along(vec))
. Right now, your code does1:seq_along(length(vec))
which is effectively1: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
add a comment |
2 Answers
2
active
oldest
votes
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.
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
add a comment |
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
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%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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 27 at 5:09
SonnySonny
2,7051 gold badge6 silver badges17 bronze badges
2,7051 gold badge6 silver badges17 bronze badges
add a comment |
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%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
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
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 withfor(k in seq_along(vec))
. Right now, your code does1:seq_along(length(vec))
which is effectively1:1
and -- I suspect -- not what is intended.– Dan Y
Mar 27 at 5:48