Specify a vector of x and y variables for purrr::map() in conjunction with dplyr::summarise_at()





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a similar dataset but with many more r and v variables.



set.seed(1000)
tb <- tibble(grp = c(rep("A",4),rep("B",4)),
v1 = rnorm(8),
v2 = rnorm(8),
v3 = rnorm(8),
r1 = rnorm(8),
r2 = rnorm(8))


For each v variable, I would like to create a lm() with r variables.



This is what I have so far:



lm_fun <- function(x,y) coef(lm(x ~ y))[2]



tb %>% 
nest(-grp) %>%
mutate(lm_list = map(data, ~ .x %>%
summarise_at(colnames(tb)[c(2:4)], funs(r1=lm_fun), .$r1)),
lm_list2= map(data, ~ .x %>%
summarise_at(colnames(tb)[c(2:4)], funs(r2=lm_fun), .$r2)),) %>%
select(grp,lm_list,lm_list2) %>%
unnest()


which gives me the intended output:



# A tibble: 2 x 7
grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840


However, how can I specify the r variables in a vector (in a similar way of specifying the v variables as colnames(tb)[...]. I don't want to copy-pasta the code for every r variable I have in my full data. Also, would it be possible to solve this with another method?



Note that it is not important that the function is performing lm(), could be any function that involves two variables.










share|improve this question





























    1















    I have a similar dataset but with many more r and v variables.



    set.seed(1000)
    tb <- tibble(grp = c(rep("A",4),rep("B",4)),
    v1 = rnorm(8),
    v2 = rnorm(8),
    v3 = rnorm(8),
    r1 = rnorm(8),
    r2 = rnorm(8))


    For each v variable, I would like to create a lm() with r variables.



    This is what I have so far:



    lm_fun <- function(x,y) coef(lm(x ~ y))[2]



    tb %>% 
    nest(-grp) %>%
    mutate(lm_list = map(data, ~ .x %>%
    summarise_at(colnames(tb)[c(2:4)], funs(r1=lm_fun), .$r1)),
    lm_list2= map(data, ~ .x %>%
    summarise_at(colnames(tb)[c(2:4)], funs(r2=lm_fun), .$r2)),) %>%
    select(grp,lm_list,lm_list2) %>%
    unnest()


    which gives me the intended output:



    # A tibble: 2 x 7
    grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
    <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
    2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840


    However, how can I specify the r variables in a vector (in a similar way of specifying the v variables as colnames(tb)[...]. I don't want to copy-pasta the code for every r variable I have in my full data. Also, would it be possible to solve this with another method?



    Note that it is not important that the function is performing lm(), could be any function that involves two variables.










    share|improve this question

























      1












      1








      1








      I have a similar dataset but with many more r and v variables.



      set.seed(1000)
      tb <- tibble(grp = c(rep("A",4),rep("B",4)),
      v1 = rnorm(8),
      v2 = rnorm(8),
      v3 = rnorm(8),
      r1 = rnorm(8),
      r2 = rnorm(8))


      For each v variable, I would like to create a lm() with r variables.



      This is what I have so far:



      lm_fun <- function(x,y) coef(lm(x ~ y))[2]



      tb %>% 
      nest(-grp) %>%
      mutate(lm_list = map(data, ~ .x %>%
      summarise_at(colnames(tb)[c(2:4)], funs(r1=lm_fun), .$r1)),
      lm_list2= map(data, ~ .x %>%
      summarise_at(colnames(tb)[c(2:4)], funs(r2=lm_fun), .$r2)),) %>%
      select(grp,lm_list,lm_list2) %>%
      unnest()


      which gives me the intended output:



      # A tibble: 2 x 7
      grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
      <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
      1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
      2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840


      However, how can I specify the r variables in a vector (in a similar way of specifying the v variables as colnames(tb)[...]. I don't want to copy-pasta the code for every r variable I have in my full data. Also, would it be possible to solve this with another method?



      Note that it is not important that the function is performing lm(), could be any function that involves two variables.










      share|improve this question














      I have a similar dataset but with many more r and v variables.



      set.seed(1000)
      tb <- tibble(grp = c(rep("A",4),rep("B",4)),
      v1 = rnorm(8),
      v2 = rnorm(8),
      v3 = rnorm(8),
      r1 = rnorm(8),
      r2 = rnorm(8))


      For each v variable, I would like to create a lm() with r variables.



      This is what I have so far:



      lm_fun <- function(x,y) coef(lm(x ~ y))[2]



      tb %>% 
      nest(-grp) %>%
      mutate(lm_list = map(data, ~ .x %>%
      summarise_at(colnames(tb)[c(2:4)], funs(r1=lm_fun), .$r1)),
      lm_list2= map(data, ~ .x %>%
      summarise_at(colnames(tb)[c(2:4)], funs(r2=lm_fun), .$r2)),) %>%
      select(grp,lm_list,lm_list2) %>%
      unnest()


      which gives me the intended output:



      # A tibble: 2 x 7
      grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
      <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
      1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
      2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840


      However, how can I specify the r variables in a vector (in a similar way of specifying the v variables as colnames(tb)[...]. I don't want to copy-pasta the code for every r variable I have in my full data. Also, would it be possible to solve this with another method?



      Note that it is not important that the function is performing lm(), could be any function that involves two variables.







      r dplyr purrr






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 18:14









      nadizannadizan

      1,060922




      1,060922
























          2 Answers
          2






          active

          oldest

          votes


















          1














          An option would be to loop through the 'r' columns inside map. This simplifies the code as we are using the same data but different 'r' columns



          library(tidyverse)
          tb %>%
          nest(-grp) %>%
          mutate(lm_list = map(data, function(x)
          map(paste0('r', 1:2), function(y)
          x %>%
          summarise_at(vars(names(.)[1:3]), funs(lm_fun), .[[y]]) %>%
          rename_all(~ paste(., y, sep="_")) ) %>%
          bind_cols)) %>%
          select(-data) %>%
          unnest
          # A tibble: 2 x 7
          # grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
          # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
          #1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
          #2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840





          share|improve this answer

































            1














            Another option would be to gather the levels of r before mutate/map:



            tb %>% 
            gather(r, value, starts_with('r')) %>%
            nest(-r, -grp) %>%
            mutate(lm_list = map(
            data, ~ .x %>%
            summarise_at(colnames(tb)[c(2:4)], funs(lm_fun), .$value)
            )) %>%
            unnest(lm_list, .drop = T)

            grp r v1 v2 v3
            <chr> <chr> <dbl> <dbl> <dbl>
            1 A r1 -0.188 -0.0972 0.858
            2 B r1 0.208 0.935 -1.33
            3 A r2 0.130 0.136 1.21
            4 B r2 -0.339 0.0580 -0.840





            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%2f53436336%2fspecify-a-vector-of-x-and-y-variables-for-purrrmap-in-conjunction-with-dplyr%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














              An option would be to loop through the 'r' columns inside map. This simplifies the code as we are using the same data but different 'r' columns



              library(tidyverse)
              tb %>%
              nest(-grp) %>%
              mutate(lm_list = map(data, function(x)
              map(paste0('r', 1:2), function(y)
              x %>%
              summarise_at(vars(names(.)[1:3]), funs(lm_fun), .[[y]]) %>%
              rename_all(~ paste(., y, sep="_")) ) %>%
              bind_cols)) %>%
              select(-data) %>%
              unnest
              # A tibble: 2 x 7
              # grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
              # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
              #1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
              #2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840





              share|improve this answer






























                1














                An option would be to loop through the 'r' columns inside map. This simplifies the code as we are using the same data but different 'r' columns



                library(tidyverse)
                tb %>%
                nest(-grp) %>%
                mutate(lm_list = map(data, function(x)
                map(paste0('r', 1:2), function(y)
                x %>%
                summarise_at(vars(names(.)[1:3]), funs(lm_fun), .[[y]]) %>%
                rename_all(~ paste(., y, sep="_")) ) %>%
                bind_cols)) %>%
                select(-data) %>%
                unnest
                # A tibble: 2 x 7
                # grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
                # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
                #1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
                #2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840





                share|improve this answer




























                  1












                  1








                  1







                  An option would be to loop through the 'r' columns inside map. This simplifies the code as we are using the same data but different 'r' columns



                  library(tidyverse)
                  tb %>%
                  nest(-grp) %>%
                  mutate(lm_list = map(data, function(x)
                  map(paste0('r', 1:2), function(y)
                  x %>%
                  summarise_at(vars(names(.)[1:3]), funs(lm_fun), .[[y]]) %>%
                  rename_all(~ paste(., y, sep="_")) ) %>%
                  bind_cols)) %>%
                  select(-data) %>%
                  unnest
                  # A tibble: 2 x 7
                  # grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
                  # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
                  #1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
                  #2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840





                  share|improve this answer















                  An option would be to loop through the 'r' columns inside map. This simplifies the code as we are using the same data but different 'r' columns



                  library(tidyverse)
                  tb %>%
                  nest(-grp) %>%
                  mutate(lm_list = map(data, function(x)
                  map(paste0('r', 1:2), function(y)
                  x %>%
                  summarise_at(vars(names(.)[1:3]), funs(lm_fun), .[[y]]) %>%
                  rename_all(~ paste(., y, sep="_")) ) %>%
                  bind_cols)) %>%
                  select(-data) %>%
                  unnest
                  # A tibble: 2 x 7
                  # grp v1_r1 v2_r1 v3_r1 v1_r2 v2_r2 v3_r2
                  # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
                  #1 A -0.188 -0.0972 0.858 0.130 0.136 1.21
                  #2 B 0.208 0.935 -1.33 -0.339 0.0580 -0.840






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 18:47

























                  answered Nov 22 '18 at 18:31









                  akrunakrun

                  422k13209284




                  422k13209284

























                      1














                      Another option would be to gather the levels of r before mutate/map:



                      tb %>% 
                      gather(r, value, starts_with('r')) %>%
                      nest(-r, -grp) %>%
                      mutate(lm_list = map(
                      data, ~ .x %>%
                      summarise_at(colnames(tb)[c(2:4)], funs(lm_fun), .$value)
                      )) %>%
                      unnest(lm_list, .drop = T)

                      grp r v1 v2 v3
                      <chr> <chr> <dbl> <dbl> <dbl>
                      1 A r1 -0.188 -0.0972 0.858
                      2 B r1 0.208 0.935 -1.33
                      3 A r2 0.130 0.136 1.21
                      4 B r2 -0.339 0.0580 -0.840





                      share|improve this answer




























                        1














                        Another option would be to gather the levels of r before mutate/map:



                        tb %>% 
                        gather(r, value, starts_with('r')) %>%
                        nest(-r, -grp) %>%
                        mutate(lm_list = map(
                        data, ~ .x %>%
                        summarise_at(colnames(tb)[c(2:4)], funs(lm_fun), .$value)
                        )) %>%
                        unnest(lm_list, .drop = T)

                        grp r v1 v2 v3
                        <chr> <chr> <dbl> <dbl> <dbl>
                        1 A r1 -0.188 -0.0972 0.858
                        2 B r1 0.208 0.935 -1.33
                        3 A r2 0.130 0.136 1.21
                        4 B r2 -0.339 0.0580 -0.840





                        share|improve this answer


























                          1












                          1








                          1







                          Another option would be to gather the levels of r before mutate/map:



                          tb %>% 
                          gather(r, value, starts_with('r')) %>%
                          nest(-r, -grp) %>%
                          mutate(lm_list = map(
                          data, ~ .x %>%
                          summarise_at(colnames(tb)[c(2:4)], funs(lm_fun), .$value)
                          )) %>%
                          unnest(lm_list, .drop = T)

                          grp r v1 v2 v3
                          <chr> <chr> <dbl> <dbl> <dbl>
                          1 A r1 -0.188 -0.0972 0.858
                          2 B r1 0.208 0.935 -1.33
                          3 A r2 0.130 0.136 1.21
                          4 B r2 -0.339 0.0580 -0.840





                          share|improve this answer













                          Another option would be to gather the levels of r before mutate/map:



                          tb %>% 
                          gather(r, value, starts_with('r')) %>%
                          nest(-r, -grp) %>%
                          mutate(lm_list = map(
                          data, ~ .x %>%
                          summarise_at(colnames(tb)[c(2:4)], funs(lm_fun), .$value)
                          )) %>%
                          unnest(lm_list, .drop = T)

                          grp r v1 v2 v3
                          <chr> <chr> <dbl> <dbl> <dbl>
                          1 A r1 -0.188 -0.0972 0.858
                          2 B r1 0.208 0.935 -1.33
                          3 A r2 0.130 0.136 1.21
                          4 B r2 -0.339 0.0580 -0.840






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 22 '18 at 18:33









                          jdobresjdobres

                          5,0781623




                          5,0781623






























                              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%2f53436336%2fspecify-a-vector-of-x-and-y-variables-for-purrrmap-in-conjunction-with-dplyr%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

                              How to change which sound is reproduced for terminal bell?

                              Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                              Can I use Tabulator js library in my java Spring + Thymeleaf project?