Loop for variable names in R












0















I'm trying to do a loop in the following way:



for(i in 1:38){
brdf$g[[i]]<-ifelse(brdf$C[[i]]==4,brdf$P[[i]],0)
brdf$g[[i]]<-max(brdf$g_[[i]])
}


In the data frame brdf, I already have C1, C2...,C38 and P1, P2...,P38. Now I want to create variables g1, g2,...g38 so that g1=P1 if C1==4, g2=P2 if C2==4 ,...g38=P38 if C38==4.



When i try to run the code above the error is:



Error in g_[i] <- ifelse(brdf$C[i] == 4, brdf$P[i], 0) : 
replacement has length zero


What is the problem?










share|improve this question

























  • Creating variables iteratively is usually unadvised (cluttering of the environment / potential instability?). Perhaps you meant to append values to an existing vector? You can do so via: g <- c(g, new_value). If you're dealing with a lot of data, pre-allocating the vector before the for loop might be beneficial.

    – 12b345b6b78
    Nov 16 '18 at 0:45






  • 1





    Replace your single square brackets by double brackets

    – prosoitos
    Nov 16 '18 at 0:47













  • Solve the error, but the variables were not created in the data frame

    – arabelo
    Nov 16 '18 at 0:57













  • it only apperas g_ in data, with values of "logic()". Is there a better way that can i loop this?

    – arabelo
    Nov 16 '18 at 1:38











  • Can you post an example of your data? Just a toy example would suffice, with what's expected.

    – RLave
    Nov 16 '18 at 8:25


















0















I'm trying to do a loop in the following way:



for(i in 1:38){
brdf$g[[i]]<-ifelse(brdf$C[[i]]==4,brdf$P[[i]],0)
brdf$g[[i]]<-max(brdf$g_[[i]])
}


In the data frame brdf, I already have C1, C2...,C38 and P1, P2...,P38. Now I want to create variables g1, g2,...g38 so that g1=P1 if C1==4, g2=P2 if C2==4 ,...g38=P38 if C38==4.



When i try to run the code above the error is:



Error in g_[i] <- ifelse(brdf$C[i] == 4, brdf$P[i], 0) : 
replacement has length zero


What is the problem?










share|improve this question

























  • Creating variables iteratively is usually unadvised (cluttering of the environment / potential instability?). Perhaps you meant to append values to an existing vector? You can do so via: g <- c(g, new_value). If you're dealing with a lot of data, pre-allocating the vector before the for loop might be beneficial.

    – 12b345b6b78
    Nov 16 '18 at 0:45






  • 1





    Replace your single square brackets by double brackets

    – prosoitos
    Nov 16 '18 at 0:47













  • Solve the error, but the variables were not created in the data frame

    – arabelo
    Nov 16 '18 at 0:57













  • it only apperas g_ in data, with values of "logic()". Is there a better way that can i loop this?

    – arabelo
    Nov 16 '18 at 1:38











  • Can you post an example of your data? Just a toy example would suffice, with what's expected.

    – RLave
    Nov 16 '18 at 8:25
















0












0








0








I'm trying to do a loop in the following way:



for(i in 1:38){
brdf$g[[i]]<-ifelse(brdf$C[[i]]==4,brdf$P[[i]],0)
brdf$g[[i]]<-max(brdf$g_[[i]])
}


In the data frame brdf, I already have C1, C2...,C38 and P1, P2...,P38. Now I want to create variables g1, g2,...g38 so that g1=P1 if C1==4, g2=P2 if C2==4 ,...g38=P38 if C38==4.



When i try to run the code above the error is:



Error in g_[i] <- ifelse(brdf$C[i] == 4, brdf$P[i], 0) : 
replacement has length zero


What is the problem?










share|improve this question
















I'm trying to do a loop in the following way:



for(i in 1:38){
brdf$g[[i]]<-ifelse(brdf$C[[i]]==4,brdf$P[[i]],0)
brdf$g[[i]]<-max(brdf$g_[[i]])
}


In the data frame brdf, I already have C1, C2...,C38 and P1, P2...,P38. Now I want to create variables g1, g2,...g38 so that g1=P1 if C1==4, g2=P2 if C2==4 ,...g38=P38 if C38==4.



When i try to run the code above the error is:



Error in g_[i] <- ifelse(brdf$C[i] == 4, brdf$P[i], 0) : 
replacement has length zero


What is the problem?







r loops for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 0:50







arabelo

















asked Nov 16 '18 at 0:42









arabeloarabelo

255




255













  • Creating variables iteratively is usually unadvised (cluttering of the environment / potential instability?). Perhaps you meant to append values to an existing vector? You can do so via: g <- c(g, new_value). If you're dealing with a lot of data, pre-allocating the vector before the for loop might be beneficial.

    – 12b345b6b78
    Nov 16 '18 at 0:45






  • 1





    Replace your single square brackets by double brackets

    – prosoitos
    Nov 16 '18 at 0:47













  • Solve the error, but the variables were not created in the data frame

    – arabelo
    Nov 16 '18 at 0:57













  • it only apperas g_ in data, with values of "logic()". Is there a better way that can i loop this?

    – arabelo
    Nov 16 '18 at 1:38











  • Can you post an example of your data? Just a toy example would suffice, with what's expected.

    – RLave
    Nov 16 '18 at 8:25





















  • Creating variables iteratively is usually unadvised (cluttering of the environment / potential instability?). Perhaps you meant to append values to an existing vector? You can do so via: g <- c(g, new_value). If you're dealing with a lot of data, pre-allocating the vector before the for loop might be beneficial.

    – 12b345b6b78
    Nov 16 '18 at 0:45






  • 1





    Replace your single square brackets by double brackets

    – prosoitos
    Nov 16 '18 at 0:47













  • Solve the error, but the variables were not created in the data frame

    – arabelo
    Nov 16 '18 at 0:57













  • it only apperas g_ in data, with values of "logic()". Is there a better way that can i loop this?

    – arabelo
    Nov 16 '18 at 1:38











  • Can you post an example of your data? Just a toy example would suffice, with what's expected.

    – RLave
    Nov 16 '18 at 8:25



















Creating variables iteratively is usually unadvised (cluttering of the environment / potential instability?). Perhaps you meant to append values to an existing vector? You can do so via: g <- c(g, new_value). If you're dealing with a lot of data, pre-allocating the vector before the for loop might be beneficial.

– 12b345b6b78
Nov 16 '18 at 0:45





Creating variables iteratively is usually unadvised (cluttering of the environment / potential instability?). Perhaps you meant to append values to an existing vector? You can do so via: g <- c(g, new_value). If you're dealing with a lot of data, pre-allocating the vector before the for loop might be beneficial.

– 12b345b6b78
Nov 16 '18 at 0:45




1




1





Replace your single square brackets by double brackets

– prosoitos
Nov 16 '18 at 0:47







Replace your single square brackets by double brackets

– prosoitos
Nov 16 '18 at 0:47















Solve the error, but the variables were not created in the data frame

– arabelo
Nov 16 '18 at 0:57







Solve the error, but the variables were not created in the data frame

– arabelo
Nov 16 '18 at 0:57















it only apperas g_ in data, with values of "logic()". Is there a better way that can i loop this?

– arabelo
Nov 16 '18 at 1:38





it only apperas g_ in data, with values of "logic()". Is there a better way that can i loop this?

– arabelo
Nov 16 '18 at 1:38













Can you post an example of your data? Just a toy example would suffice, with what's expected.

– RLave
Nov 16 '18 at 8:25







Can you post an example of your data? Just a toy example would suffice, with what's expected.

– RLave
Nov 16 '18 at 8:25














0






active

oldest

votes











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%2f53329842%2floop-for-variable-names-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53329842%2floop-for-variable-names-in-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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python