Mutate by condition with many columns with each one a different setting












3















I have looking for but not found how make a simple if for many columns in dplyr.



I have this code (it works):



library(dplyr)
library(magrittr)
data("PlantGrowth")
PlantGrowth %>% mutate (
a=if_else(group=="ctrl", weight*2, weight*100),
b=if_else(group=="ctrl", weight*1,5, weight/100),
c=if_else(group=="ctrl", weight*4, weight*100),
d=if_else(group=="ctrl", weight*5, weight/1000)
)


And I would like to not repeat the condition. Something like that:



PlantGrowth %>% mutate_if_foo (
group=="ctrl",{
a=weight*2,
b=weight*1,5,
c=weight*4,
d=weight*5
}
)%>% mutate_if_foo (
group!="ctrl",{
a=weight*100,
b=weight/100),
c=weight*100),
d=weight/1000)
}
)


I've found many answers on mutate_if,mutate_all, mutate_at , case_when but they don't answer at my question.



Please with dplyr / tidyverse.



Thanks in advance



EDIT



I've tried, from @Rohit_das idea about functions.



mtcars %>% ( function(df) { 
if (df$am==1){
df%>% mutate(
a=df$mpg*3,
b=df$cyl*10)
}else{
df%>% mutate(
a=df$disp*300,
d=df$cyl*1000)
}
})


but I have Warning message:



In if (df$am == 1) { : 
the condition has length > 1
and only the first element will be used









share|improve this question





























    3















    I have looking for but not found how make a simple if for many columns in dplyr.



    I have this code (it works):



    library(dplyr)
    library(magrittr)
    data("PlantGrowth")
    PlantGrowth %>% mutate (
    a=if_else(group=="ctrl", weight*2, weight*100),
    b=if_else(group=="ctrl", weight*1,5, weight/100),
    c=if_else(group=="ctrl", weight*4, weight*100),
    d=if_else(group=="ctrl", weight*5, weight/1000)
    )


    And I would like to not repeat the condition. Something like that:



    PlantGrowth %>% mutate_if_foo (
    group=="ctrl",{
    a=weight*2,
    b=weight*1,5,
    c=weight*4,
    d=weight*5
    }
    )%>% mutate_if_foo (
    group!="ctrl",{
    a=weight*100,
    b=weight/100),
    c=weight*100),
    d=weight/1000)
    }
    )


    I've found many answers on mutate_if,mutate_all, mutate_at , case_when but they don't answer at my question.



    Please with dplyr / tidyverse.



    Thanks in advance



    EDIT



    I've tried, from @Rohit_das idea about functions.



    mtcars %>% ( function(df) { 
    if (df$am==1){
    df%>% mutate(
    a=df$mpg*3,
    b=df$cyl*10)
    }else{
    df%>% mutate(
    a=df$disp*300,
    d=df$cyl*1000)
    }
    })


    but I have Warning message:



    In if (df$am == 1) { : 
    the condition has length > 1
    and only the first element will be used









    share|improve this question



























      3












      3








      3


      1






      I have looking for but not found how make a simple if for many columns in dplyr.



      I have this code (it works):



      library(dplyr)
      library(magrittr)
      data("PlantGrowth")
      PlantGrowth %>% mutate (
      a=if_else(group=="ctrl", weight*2, weight*100),
      b=if_else(group=="ctrl", weight*1,5, weight/100),
      c=if_else(group=="ctrl", weight*4, weight*100),
      d=if_else(group=="ctrl", weight*5, weight/1000)
      )


      And I would like to not repeat the condition. Something like that:



      PlantGrowth %>% mutate_if_foo (
      group=="ctrl",{
      a=weight*2,
      b=weight*1,5,
      c=weight*4,
      d=weight*5
      }
      )%>% mutate_if_foo (
      group!="ctrl",{
      a=weight*100,
      b=weight/100),
      c=weight*100),
      d=weight/1000)
      }
      )


      I've found many answers on mutate_if,mutate_all, mutate_at , case_when but they don't answer at my question.



      Please with dplyr / tidyverse.



      Thanks in advance



      EDIT



      I've tried, from @Rohit_das idea about functions.



      mtcars %>% ( function(df) { 
      if (df$am==1){
      df%>% mutate(
      a=df$mpg*3,
      b=df$cyl*10)
      }else{
      df%>% mutate(
      a=df$disp*300,
      d=df$cyl*1000)
      }
      })


      but I have Warning message:



      In if (df$am == 1) { : 
      the condition has length > 1
      and only the first element will be used









      share|improve this question
















      I have looking for but not found how make a simple if for many columns in dplyr.



      I have this code (it works):



      library(dplyr)
      library(magrittr)
      data("PlantGrowth")
      PlantGrowth %>% mutate (
      a=if_else(group=="ctrl", weight*2, weight*100),
      b=if_else(group=="ctrl", weight*1,5, weight/100),
      c=if_else(group=="ctrl", weight*4, weight*100),
      d=if_else(group=="ctrl", weight*5, weight/1000)
      )


      And I would like to not repeat the condition. Something like that:



      PlantGrowth %>% mutate_if_foo (
      group=="ctrl",{
      a=weight*2,
      b=weight*1,5,
      c=weight*4,
      d=weight*5
      }
      )%>% mutate_if_foo (
      group!="ctrl",{
      a=weight*100,
      b=weight/100),
      c=weight*100),
      d=weight/1000)
      }
      )


      I've found many answers on mutate_if,mutate_all, mutate_at , case_when but they don't answer at my question.



      Please with dplyr / tidyverse.



      Thanks in advance



      EDIT



      I've tried, from @Rohit_das idea about functions.



      mtcars %>% ( function(df) { 
      if (df$am==1){
      df%>% mutate(
      a=df$mpg*3,
      b=df$cyl*10)
      }else{
      df%>% mutate(
      a=df$disp*300,
      d=df$cyl*1000)
      }
      })


      but I have Warning message:



      In if (df$am == 1) { : 
      the condition has length > 1
      and only the first element will be used






      r dplyr conditional mutate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 11:10







      phili_b

















      asked Nov 15 '18 at 23:11









      phili_bphili_b

      9611




      9611
























          3 Answers
          3






          active

          oldest

          votes


















          1














          Not sure I understand the issue here. If you just want to reduce the verbosity of the code then just create a custom function



          customif = function(x,y) { 
          if_else(group=="ctrl", weight*x, weight*y)
          }


          then you can call this function in your mutate as



          PlantGrowth %>% mutate (
          a=customif(2,100),
          b=customif(1,5, 1/100),
          c=customif(4, 100),
          d=customif(5, 1/1000)
          )





          share|improve this answer
























          • Custom function could be a solution, thanks for this example, but your solution is not what I am looking for. I've taken simple operation but in reality it could be more complex and not between the same n-upplet. I am looking for distribute each different condition one time only by condition, and perhaps distribute your custom function, one time only by condition. In procedural it's possible but I'm looking for dplyr flow solution: maybe it's not made for that, like in SQL. It's true : only to reduce the verbosity.

            – phili_b
            Nov 16 '18 at 7:24



















          0














          I think I found a neat solution with purrr. It takes a data frame of inputs and then dynamically names new columns a:d with new inputs for each column. First column will use x = 2, y = 100 and z = "a" and then the next row, and so on. The cool thing with functional programming like this is that it is very easy to scale up.



          library(tidyverse)

          iterate <- tibble(x = c(2, 1.5, 4, 5),
          y = c(100, 1/100, 100, 1/1000),
          z = c("a", "b", "c", "d"))

          fun <- function(x, y, z) {
          PlantGrowth %>%
          mutate(!!z := if_else(group == "ctrl", weight * x, weight * y)) %>%
          select(3)
          }

          PlantGrowth %>%
          bind_cols(
          pmap_dfc(iterate, fun)
          ) %>%
          as_tibble


          Which gives you the same df:



          # A tibble: 30 x 6
          weight group a b c d
          <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
          1 4.17 ctrl 8.34 6.26 16.7 20.8
          2 5.58 ctrl 11.2 8.37 22.3 27.9
          3 5.18 ctrl 10.4 7.77 20.7 25.9
          4 6.11 ctrl 12.2 9.17 24.4 30.6
          5 4.5 ctrl 9 6.75 18 22.5





          share|improve this answer
























          • It's a beautiful juice of brain, but in this case, it's more complicated than the initial code :p

            – phili_b
            Nov 16 '18 at 8:28











          • @phili_b That is true, for sure! It should only be used when it makes the code easier. If you ever find that you need to do the same logic over more than 4 columns and the code becomes to "long", it helps to keep the mind sane :)

            – davsjob
            Nov 16 '18 at 8:33



















          0














          I think I've found an answer. I tested on mtcars. I didn't test yet on my real code.



          Comment please if I you think I am wrong in the concept.



          The conditions of the filters have to be exclusives else I will take duplicate lines.



                  library(dplyr)
          library(magrittr)
          library(tibble) # only if necessary to preserve rownames
          mtcars %>% ( function(df) {
          rbind(
          (df
          %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
          %>%dplyr::filter(am==1)
          %>%dplyr::mutate(
          a=mpg*3,
          b=cyl*10,d=NA)),
          (df
          %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
          %>%dplyr::filter(am!=1)
          %>%dplyr::mutate(
          a=disp*3,
          d=cyl*100,b=NA))
          )
          }) %>%arrange(rowid)





          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%2f53329170%2fmutate-by-condition-with-many-columns-with-each-one-a-different-setting%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Not sure I understand the issue here. If you just want to reduce the verbosity of the code then just create a custom function



            customif = function(x,y) { 
            if_else(group=="ctrl", weight*x, weight*y)
            }


            then you can call this function in your mutate as



            PlantGrowth %>% mutate (
            a=customif(2,100),
            b=customif(1,5, 1/100),
            c=customif(4, 100),
            d=customif(5, 1/1000)
            )





            share|improve this answer
























            • Custom function could be a solution, thanks for this example, but your solution is not what I am looking for. I've taken simple operation but in reality it could be more complex and not between the same n-upplet. I am looking for distribute each different condition one time only by condition, and perhaps distribute your custom function, one time only by condition. In procedural it's possible but I'm looking for dplyr flow solution: maybe it's not made for that, like in SQL. It's true : only to reduce the verbosity.

              – phili_b
              Nov 16 '18 at 7:24
















            1














            Not sure I understand the issue here. If you just want to reduce the verbosity of the code then just create a custom function



            customif = function(x,y) { 
            if_else(group=="ctrl", weight*x, weight*y)
            }


            then you can call this function in your mutate as



            PlantGrowth %>% mutate (
            a=customif(2,100),
            b=customif(1,5, 1/100),
            c=customif(4, 100),
            d=customif(5, 1/1000)
            )





            share|improve this answer
























            • Custom function could be a solution, thanks for this example, but your solution is not what I am looking for. I've taken simple operation but in reality it could be more complex and not between the same n-upplet. I am looking for distribute each different condition one time only by condition, and perhaps distribute your custom function, one time only by condition. In procedural it's possible but I'm looking for dplyr flow solution: maybe it's not made for that, like in SQL. It's true : only to reduce the verbosity.

              – phili_b
              Nov 16 '18 at 7:24














            1












            1








            1







            Not sure I understand the issue here. If you just want to reduce the verbosity of the code then just create a custom function



            customif = function(x,y) { 
            if_else(group=="ctrl", weight*x, weight*y)
            }


            then you can call this function in your mutate as



            PlantGrowth %>% mutate (
            a=customif(2,100),
            b=customif(1,5, 1/100),
            c=customif(4, 100),
            d=customif(5, 1/1000)
            )





            share|improve this answer













            Not sure I understand the issue here. If you just want to reduce the verbosity of the code then just create a custom function



            customif = function(x,y) { 
            if_else(group=="ctrl", weight*x, weight*y)
            }


            then you can call this function in your mutate as



            PlantGrowth %>% mutate (
            a=customif(2,100),
            b=customif(1,5, 1/100),
            c=customif(4, 100),
            d=customif(5, 1/1000)
            )






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 23:39









            Rohit DasRohit Das

            1,372918




            1,372918













            • Custom function could be a solution, thanks for this example, but your solution is not what I am looking for. I've taken simple operation but in reality it could be more complex and not between the same n-upplet. I am looking for distribute each different condition one time only by condition, and perhaps distribute your custom function, one time only by condition. In procedural it's possible but I'm looking for dplyr flow solution: maybe it's not made for that, like in SQL. It's true : only to reduce the verbosity.

              – phili_b
              Nov 16 '18 at 7:24



















            • Custom function could be a solution, thanks for this example, but your solution is not what I am looking for. I've taken simple operation but in reality it could be more complex and not between the same n-upplet. I am looking for distribute each different condition one time only by condition, and perhaps distribute your custom function, one time only by condition. In procedural it's possible but I'm looking for dplyr flow solution: maybe it's not made for that, like in SQL. It's true : only to reduce the verbosity.

              – phili_b
              Nov 16 '18 at 7:24

















            Custom function could be a solution, thanks for this example, but your solution is not what I am looking for. I've taken simple operation but in reality it could be more complex and not between the same n-upplet. I am looking for distribute each different condition one time only by condition, and perhaps distribute your custom function, one time only by condition. In procedural it's possible but I'm looking for dplyr flow solution: maybe it's not made for that, like in SQL. It's true : only to reduce the verbosity.

            – phili_b
            Nov 16 '18 at 7:24





            Custom function could be a solution, thanks for this example, but your solution is not what I am looking for. I've taken simple operation but in reality it could be more complex and not between the same n-upplet. I am looking for distribute each different condition one time only by condition, and perhaps distribute your custom function, one time only by condition. In procedural it's possible but I'm looking for dplyr flow solution: maybe it's not made for that, like in SQL. It's true : only to reduce the verbosity.

            – phili_b
            Nov 16 '18 at 7:24













            0














            I think I found a neat solution with purrr. It takes a data frame of inputs and then dynamically names new columns a:d with new inputs for each column. First column will use x = 2, y = 100 and z = "a" and then the next row, and so on. The cool thing with functional programming like this is that it is very easy to scale up.



            library(tidyverse)

            iterate <- tibble(x = c(2, 1.5, 4, 5),
            y = c(100, 1/100, 100, 1/1000),
            z = c("a", "b", "c", "d"))

            fun <- function(x, y, z) {
            PlantGrowth %>%
            mutate(!!z := if_else(group == "ctrl", weight * x, weight * y)) %>%
            select(3)
            }

            PlantGrowth %>%
            bind_cols(
            pmap_dfc(iterate, fun)
            ) %>%
            as_tibble


            Which gives you the same df:



            # A tibble: 30 x 6
            weight group a b c d
            <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
            1 4.17 ctrl 8.34 6.26 16.7 20.8
            2 5.58 ctrl 11.2 8.37 22.3 27.9
            3 5.18 ctrl 10.4 7.77 20.7 25.9
            4 6.11 ctrl 12.2 9.17 24.4 30.6
            5 4.5 ctrl 9 6.75 18 22.5





            share|improve this answer
























            • It's a beautiful juice of brain, but in this case, it's more complicated than the initial code :p

              – phili_b
              Nov 16 '18 at 8:28











            • @phili_b That is true, for sure! It should only be used when it makes the code easier. If you ever find that you need to do the same logic over more than 4 columns and the code becomes to "long", it helps to keep the mind sane :)

              – davsjob
              Nov 16 '18 at 8:33
















            0














            I think I found a neat solution with purrr. It takes a data frame of inputs and then dynamically names new columns a:d with new inputs for each column. First column will use x = 2, y = 100 and z = "a" and then the next row, and so on. The cool thing with functional programming like this is that it is very easy to scale up.



            library(tidyverse)

            iterate <- tibble(x = c(2, 1.5, 4, 5),
            y = c(100, 1/100, 100, 1/1000),
            z = c("a", "b", "c", "d"))

            fun <- function(x, y, z) {
            PlantGrowth %>%
            mutate(!!z := if_else(group == "ctrl", weight * x, weight * y)) %>%
            select(3)
            }

            PlantGrowth %>%
            bind_cols(
            pmap_dfc(iterate, fun)
            ) %>%
            as_tibble


            Which gives you the same df:



            # A tibble: 30 x 6
            weight group a b c d
            <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
            1 4.17 ctrl 8.34 6.26 16.7 20.8
            2 5.58 ctrl 11.2 8.37 22.3 27.9
            3 5.18 ctrl 10.4 7.77 20.7 25.9
            4 6.11 ctrl 12.2 9.17 24.4 30.6
            5 4.5 ctrl 9 6.75 18 22.5





            share|improve this answer
























            • It's a beautiful juice of brain, but in this case, it's more complicated than the initial code :p

              – phili_b
              Nov 16 '18 at 8:28











            • @phili_b That is true, for sure! It should only be used when it makes the code easier. If you ever find that you need to do the same logic over more than 4 columns and the code becomes to "long", it helps to keep the mind sane :)

              – davsjob
              Nov 16 '18 at 8:33














            0












            0








            0







            I think I found a neat solution with purrr. It takes a data frame of inputs and then dynamically names new columns a:d with new inputs for each column. First column will use x = 2, y = 100 and z = "a" and then the next row, and so on. The cool thing with functional programming like this is that it is very easy to scale up.



            library(tidyverse)

            iterate <- tibble(x = c(2, 1.5, 4, 5),
            y = c(100, 1/100, 100, 1/1000),
            z = c("a", "b", "c", "d"))

            fun <- function(x, y, z) {
            PlantGrowth %>%
            mutate(!!z := if_else(group == "ctrl", weight * x, weight * y)) %>%
            select(3)
            }

            PlantGrowth %>%
            bind_cols(
            pmap_dfc(iterate, fun)
            ) %>%
            as_tibble


            Which gives you the same df:



            # A tibble: 30 x 6
            weight group a b c d
            <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
            1 4.17 ctrl 8.34 6.26 16.7 20.8
            2 5.58 ctrl 11.2 8.37 22.3 27.9
            3 5.18 ctrl 10.4 7.77 20.7 25.9
            4 6.11 ctrl 12.2 9.17 24.4 30.6
            5 4.5 ctrl 9 6.75 18 22.5





            share|improve this answer













            I think I found a neat solution with purrr. It takes a data frame of inputs and then dynamically names new columns a:d with new inputs for each column. First column will use x = 2, y = 100 and z = "a" and then the next row, and so on. The cool thing with functional programming like this is that it is very easy to scale up.



            library(tidyverse)

            iterate <- tibble(x = c(2, 1.5, 4, 5),
            y = c(100, 1/100, 100, 1/1000),
            z = c("a", "b", "c", "d"))

            fun <- function(x, y, z) {
            PlantGrowth %>%
            mutate(!!z := if_else(group == "ctrl", weight * x, weight * y)) %>%
            select(3)
            }

            PlantGrowth %>%
            bind_cols(
            pmap_dfc(iterate, fun)
            ) %>%
            as_tibble


            Which gives you the same df:



            # A tibble: 30 x 6
            weight group a b c d
            <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
            1 4.17 ctrl 8.34 6.26 16.7 20.8
            2 5.58 ctrl 11.2 8.37 22.3 27.9
            3 5.18 ctrl 10.4 7.77 20.7 25.9
            4 6.11 ctrl 12.2 9.17 24.4 30.6
            5 4.5 ctrl 9 6.75 18 22.5






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 7:31









            davsjobdavsjob

            73237




            73237













            • It's a beautiful juice of brain, but in this case, it's more complicated than the initial code :p

              – phili_b
              Nov 16 '18 at 8:28











            • @phili_b That is true, for sure! It should only be used when it makes the code easier. If you ever find that you need to do the same logic over more than 4 columns and the code becomes to "long", it helps to keep the mind sane :)

              – davsjob
              Nov 16 '18 at 8:33



















            • It's a beautiful juice of brain, but in this case, it's more complicated than the initial code :p

              – phili_b
              Nov 16 '18 at 8:28











            • @phili_b That is true, for sure! It should only be used when it makes the code easier. If you ever find that you need to do the same logic over more than 4 columns and the code becomes to "long", it helps to keep the mind sane :)

              – davsjob
              Nov 16 '18 at 8:33

















            It's a beautiful juice of brain, but in this case, it's more complicated than the initial code :p

            – phili_b
            Nov 16 '18 at 8:28





            It's a beautiful juice of brain, but in this case, it's more complicated than the initial code :p

            – phili_b
            Nov 16 '18 at 8:28













            @phili_b That is true, for sure! It should only be used when it makes the code easier. If you ever find that you need to do the same logic over more than 4 columns and the code becomes to "long", it helps to keep the mind sane :)

            – davsjob
            Nov 16 '18 at 8:33





            @phili_b That is true, for sure! It should only be used when it makes the code easier. If you ever find that you need to do the same logic over more than 4 columns and the code becomes to "long", it helps to keep the mind sane :)

            – davsjob
            Nov 16 '18 at 8:33











            0














            I think I've found an answer. I tested on mtcars. I didn't test yet on my real code.



            Comment please if I you think I am wrong in the concept.



            The conditions of the filters have to be exclusives else I will take duplicate lines.



                    library(dplyr)
            library(magrittr)
            library(tibble) # only if necessary to preserve rownames
            mtcars %>% ( function(df) {
            rbind(
            (df
            %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
            %>%dplyr::filter(am==1)
            %>%dplyr::mutate(
            a=mpg*3,
            b=cyl*10,d=NA)),
            (df
            %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
            %>%dplyr::filter(am!=1)
            %>%dplyr::mutate(
            a=disp*3,
            d=cyl*100,b=NA))
            )
            }) %>%arrange(rowid)





            share|improve this answer






























              0














              I think I've found an answer. I tested on mtcars. I didn't test yet on my real code.



              Comment please if I you think I am wrong in the concept.



              The conditions of the filters have to be exclusives else I will take duplicate lines.



                      library(dplyr)
              library(magrittr)
              library(tibble) # only if necessary to preserve rownames
              mtcars %>% ( function(df) {
              rbind(
              (df
              %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
              %>%dplyr::filter(am==1)
              %>%dplyr::mutate(
              a=mpg*3,
              b=cyl*10,d=NA)),
              (df
              %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
              %>%dplyr::filter(am!=1)
              %>%dplyr::mutate(
              a=disp*3,
              d=cyl*100,b=NA))
              )
              }) %>%arrange(rowid)





              share|improve this answer




























                0












                0








                0







                I think I've found an answer. I tested on mtcars. I didn't test yet on my real code.



                Comment please if I you think I am wrong in the concept.



                The conditions of the filters have to be exclusives else I will take duplicate lines.



                        library(dplyr)
                library(magrittr)
                library(tibble) # only if necessary to preserve rownames
                mtcars %>% ( function(df) {
                rbind(
                (df
                %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
                %>%dplyr::filter(am==1)
                %>%dplyr::mutate(
                a=mpg*3,
                b=cyl*10,d=NA)),
                (df
                %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
                %>%dplyr::filter(am!=1)
                %>%dplyr::mutate(
                a=disp*3,
                d=cyl*100,b=NA))
                )
                }) %>%arrange(rowid)





                share|improve this answer















                I think I've found an answer. I tested on mtcars. I didn't test yet on my real code.



                Comment please if I you think I am wrong in the concept.



                The conditions of the filters have to be exclusives else I will take duplicate lines.



                        library(dplyr)
                library(magrittr)
                library(tibble) # only if necessary to preserve rownames
                mtcars %>% ( function(df) {
                rbind(
                (df
                %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
                %>%dplyr::filter(am==1)
                %>%dplyr::mutate(
                a=mpg*3,
                b=cyl*10,d=NA)),
                (df
                %>% tibble::rownames_to_column(.) %>%tibble::rowid_to_column(.) # to preserve rownames
                %>%dplyr::filter(am!=1)
                %>%dplyr::mutate(
                a=disp*3,
                d=cyl*100,b=NA))
                )
                }) %>%arrange(rowid)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 19 '18 at 15:35

























                answered Nov 19 '18 at 13:27









                phili_bphili_b

                9611




                9611






























                    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%2f53329170%2fmutate-by-condition-with-many-columns-with-each-one-a-different-setting%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

                    Bressuire

                    Vorschmack

                    Quarantine