creating a new variable using conditional to another variable in a differente dataframe









up vote
1
down vote

favorite












I have the following dataframes



df1:



 round mun1 mun2
1 SP PA
1 RJ PR
1 BH BA
2 BA SP
2 PR BH
2 PA RJ
3 RJ BH
3 PA PR
3 SP BA


df2:



mun p01 p02 p03
SP 3 4 7
RJ 0 3 4
BH 3 6 9
BA 0 1 1
PA 1 2 3
PR 1 4 5


I need a collumn in df1, P, that equals 0 if round==1, maxvalue of p01 if round ==2 and the maxvalue of p02 if round==3.
In the real data, in df1 i have 38 rounds, and 380 lines, and in df2 i have 20 lins(each for unique mun). I tried the following loop:



p <-matrix(0, nrow=380,ncol=1)

for(i in 2:38)
p <- if(round==i) max(p[[i-1]] %in% df2)



But this dont work. is there a way to do that?










share|improve this question



























    up vote
    1
    down vote

    favorite












    I have the following dataframes



    df1:



     round mun1 mun2
    1 SP PA
    1 RJ PR
    1 BH BA
    2 BA SP
    2 PR BH
    2 PA RJ
    3 RJ BH
    3 PA PR
    3 SP BA


    df2:



    mun p01 p02 p03
    SP 3 4 7
    RJ 0 3 4
    BH 3 6 9
    BA 0 1 1
    PA 1 2 3
    PR 1 4 5


    I need a collumn in df1, P, that equals 0 if round==1, maxvalue of p01 if round ==2 and the maxvalue of p02 if round==3.
    In the real data, in df1 i have 38 rounds, and 380 lines, and in df2 i have 20 lins(each for unique mun). I tried the following loop:



    p <-matrix(0, nrow=380,ncol=1)

    for(i in 2:38)
    p <- if(round==i) max(p[[i-1]] %in% df2)



    But this dont work. is there a way to do that?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have the following dataframes



      df1:



       round mun1 mun2
      1 SP PA
      1 RJ PR
      1 BH BA
      2 BA SP
      2 PR BH
      2 PA RJ
      3 RJ BH
      3 PA PR
      3 SP BA


      df2:



      mun p01 p02 p03
      SP 3 4 7
      RJ 0 3 4
      BH 3 6 9
      BA 0 1 1
      PA 1 2 3
      PR 1 4 5


      I need a collumn in df1, P, that equals 0 if round==1, maxvalue of p01 if round ==2 and the maxvalue of p02 if round==3.
      In the real data, in df1 i have 38 rounds, and 380 lines, and in df2 i have 20 lins(each for unique mun). I tried the following loop:



      p <-matrix(0, nrow=380,ncol=1)

      for(i in 2:38)
      p <- if(round==i) max(p[[i-1]] %in% df2)



      But this dont work. is there a way to do that?










      share|improve this question















      I have the following dataframes



      df1:



       round mun1 mun2
      1 SP PA
      1 RJ PR
      1 BH BA
      2 BA SP
      2 PR BH
      2 PA RJ
      3 RJ BH
      3 PA PR
      3 SP BA


      df2:



      mun p01 p02 p03
      SP 3 4 7
      RJ 0 3 4
      BH 3 6 9
      BA 0 1 1
      PA 1 2 3
      PR 1 4 5


      I need a collumn in df1, P, that equals 0 if round==1, maxvalue of p01 if round ==2 and the maxvalue of p02 if round==3.
      In the real data, in df1 i have 38 rounds, and 380 lines, and in df2 i have 20 lins(each for unique mun). I tried the following loop:



      p <-matrix(0, nrow=380,ncol=1)

      for(i in 2:38)
      p <- if(round==i) max(p[[i-1]] %in% df2)



      But this dont work. is there a way to do that?







      r dataframe conditional






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 22:46

























      asked Nov 10 at 22:32









      arabelo

      255




      255






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Using dplyr. It will work if you can merge df1 and df2 based on mun1 or mun2 and mun.



          df %>% 
          left_join(df2, by = c("mun1" = "mun")) %>% #Merging the data
          mutate(P = ifelse(round == 1, 0, #Applying the condition
          ifelse(round == 2, max(p01),
          ifelse(round == 3, max(p02), NA))))

          round mun1 mun2 p01 p02 p03 P
          1 1 SP PA 3 4 7 0
          2 1 RJ PR 0 3 4 0
          3 1 BH BA 3 6 9 0
          4 2 BA SP 0 1 1 3
          5 2 PR BH 1 4 5 3
          6 2 PA RJ 1 2 3 3
          7 3 RJ BH 0 3 4 6
          8 3 PA PR 1 2 3 6
          9 3 SP BA 3 4 7 6





          share|improve this answer




















          • This would work even if df1 and df2 have differente dimensions?
            – arabelo
            Nov 10 at 22:50










          • Yes, It will work even if df1 and df2 have different number of columns and rows. Look at ?left_join.
            – tmfmnk
            Nov 10 at 22:52











          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',
          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%2f53244079%2fcreating-a-new-variable-using-conditional-to-another-variable-in-a-differente-da%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Using dplyr. It will work if you can merge df1 and df2 based on mun1 or mun2 and mun.



          df %>% 
          left_join(df2, by = c("mun1" = "mun")) %>% #Merging the data
          mutate(P = ifelse(round == 1, 0, #Applying the condition
          ifelse(round == 2, max(p01),
          ifelse(round == 3, max(p02), NA))))

          round mun1 mun2 p01 p02 p03 P
          1 1 SP PA 3 4 7 0
          2 1 RJ PR 0 3 4 0
          3 1 BH BA 3 6 9 0
          4 2 BA SP 0 1 1 3
          5 2 PR BH 1 4 5 3
          6 2 PA RJ 1 2 3 3
          7 3 RJ BH 0 3 4 6
          8 3 PA PR 1 2 3 6
          9 3 SP BA 3 4 7 6





          share|improve this answer




















          • This would work even if df1 and df2 have differente dimensions?
            – arabelo
            Nov 10 at 22:50










          • Yes, It will work even if df1 and df2 have different number of columns and rows. Look at ?left_join.
            – tmfmnk
            Nov 10 at 22:52















          up vote
          2
          down vote



          accepted










          Using dplyr. It will work if you can merge df1 and df2 based on mun1 or mun2 and mun.



          df %>% 
          left_join(df2, by = c("mun1" = "mun")) %>% #Merging the data
          mutate(P = ifelse(round == 1, 0, #Applying the condition
          ifelse(round == 2, max(p01),
          ifelse(round == 3, max(p02), NA))))

          round mun1 mun2 p01 p02 p03 P
          1 1 SP PA 3 4 7 0
          2 1 RJ PR 0 3 4 0
          3 1 BH BA 3 6 9 0
          4 2 BA SP 0 1 1 3
          5 2 PR BH 1 4 5 3
          6 2 PA RJ 1 2 3 3
          7 3 RJ BH 0 3 4 6
          8 3 PA PR 1 2 3 6
          9 3 SP BA 3 4 7 6





          share|improve this answer




















          • This would work even if df1 and df2 have differente dimensions?
            – arabelo
            Nov 10 at 22:50










          • Yes, It will work even if df1 and df2 have different number of columns and rows. Look at ?left_join.
            – tmfmnk
            Nov 10 at 22:52













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Using dplyr. It will work if you can merge df1 and df2 based on mun1 or mun2 and mun.



          df %>% 
          left_join(df2, by = c("mun1" = "mun")) %>% #Merging the data
          mutate(P = ifelse(round == 1, 0, #Applying the condition
          ifelse(round == 2, max(p01),
          ifelse(round == 3, max(p02), NA))))

          round mun1 mun2 p01 p02 p03 P
          1 1 SP PA 3 4 7 0
          2 1 RJ PR 0 3 4 0
          3 1 BH BA 3 6 9 0
          4 2 BA SP 0 1 1 3
          5 2 PR BH 1 4 5 3
          6 2 PA RJ 1 2 3 3
          7 3 RJ BH 0 3 4 6
          8 3 PA PR 1 2 3 6
          9 3 SP BA 3 4 7 6





          share|improve this answer












          Using dplyr. It will work if you can merge df1 and df2 based on mun1 or mun2 and mun.



          df %>% 
          left_join(df2, by = c("mun1" = "mun")) %>% #Merging the data
          mutate(P = ifelse(round == 1, 0, #Applying the condition
          ifelse(round == 2, max(p01),
          ifelse(round == 3, max(p02), NA))))

          round mun1 mun2 p01 p02 p03 P
          1 1 SP PA 3 4 7 0
          2 1 RJ PR 0 3 4 0
          3 1 BH BA 3 6 9 0
          4 2 BA SP 0 1 1 3
          5 2 PR BH 1 4 5 3
          6 2 PA RJ 1 2 3 3
          7 3 RJ BH 0 3 4 6
          8 3 PA PR 1 2 3 6
          9 3 SP BA 3 4 7 6






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 22:43









          tmfmnk

          1,2841411




          1,2841411











          • This would work even if df1 and df2 have differente dimensions?
            – arabelo
            Nov 10 at 22:50










          • Yes, It will work even if df1 and df2 have different number of columns and rows. Look at ?left_join.
            – tmfmnk
            Nov 10 at 22:52

















          • This would work even if df1 and df2 have differente dimensions?
            – arabelo
            Nov 10 at 22:50










          • Yes, It will work even if df1 and df2 have different number of columns and rows. Look at ?left_join.
            – tmfmnk
            Nov 10 at 22:52
















          This would work even if df1 and df2 have differente dimensions?
          – arabelo
          Nov 10 at 22:50




          This would work even if df1 and df2 have differente dimensions?
          – arabelo
          Nov 10 at 22:50












          Yes, It will work even if df1 and df2 have different number of columns and rows. Look at ?left_join.
          – tmfmnk
          Nov 10 at 22:52





          Yes, It will work even if df1 and df2 have different number of columns and rows. Look at ?left_join.
          – tmfmnk
          Nov 10 at 22:52


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244079%2fcreating-a-new-variable-using-conditional-to-another-variable-in-a-differente-da%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto