Pass multiple columns in dataframe into function at once in R












0















After much searching, I can't seem to figure this out.
Trying to write a function that:




  • takes a data frame, db

  • groups the data frame by var1

  • returns the mean and sd by group on several different columns


Here is my function,



myfun <- function(db,var1, ...) {

var1 <- enquo(var1)
var2 <- quos(...)

for (i in var2) {

db %>%
group_by(!!var1) %>%
summarise(mean_var = mean(!!!var2))

}}


when I pass the following, nothing returns



myfun(data, group, age, bmi)


Ideally, I would like to group both age and bmi by group and return the mean and sd for each. In the future, I would like to pass many more columns from data into the function...



The output would be similar to summaryBy from doby package, but on many columns at once and would look like:



Group   age.mean    age.sd
0
1
bmi.mean bmi.sd
0
1









share|improve this question



























    0















    After much searching, I can't seem to figure this out.
    Trying to write a function that:




    • takes a data frame, db

    • groups the data frame by var1

    • returns the mean and sd by group on several different columns


    Here is my function,



    myfun <- function(db,var1, ...) {

    var1 <- enquo(var1)
    var2 <- quos(...)

    for (i in var2) {

    db %>%
    group_by(!!var1) %>%
    summarise(mean_var = mean(!!!var2))

    }}


    when I pass the following, nothing returns



    myfun(data, group, age, bmi)


    Ideally, I would like to group both age and bmi by group and return the mean and sd for each. In the future, I would like to pass many more columns from data into the function...



    The output would be similar to summaryBy from doby package, but on many columns at once and would look like:



    Group   age.mean    age.sd
    0
    1
    bmi.mean bmi.sd
    0
    1









    share|improve this question

























      0












      0








      0








      After much searching, I can't seem to figure this out.
      Trying to write a function that:




      • takes a data frame, db

      • groups the data frame by var1

      • returns the mean and sd by group on several different columns


      Here is my function,



      myfun <- function(db,var1, ...) {

      var1 <- enquo(var1)
      var2 <- quos(...)

      for (i in var2) {

      db %>%
      group_by(!!var1) %>%
      summarise(mean_var = mean(!!!var2))

      }}


      when I pass the following, nothing returns



      myfun(data, group, age, bmi)


      Ideally, I would like to group both age and bmi by group and return the mean and sd for each. In the future, I would like to pass many more columns from data into the function...



      The output would be similar to summaryBy from doby package, but on many columns at once and would look like:



      Group   age.mean    age.sd
      0
      1
      bmi.mean bmi.sd
      0
      1









      share|improve this question














      After much searching, I can't seem to figure this out.
      Trying to write a function that:




      • takes a data frame, db

      • groups the data frame by var1

      • returns the mean and sd by group on several different columns


      Here is my function,



      myfun <- function(db,var1, ...) {

      var1 <- enquo(var1)
      var2 <- quos(...)

      for (i in var2) {

      db %>%
      group_by(!!var1) %>%
      summarise(mean_var = mean(!!!var2))

      }}


      when I pass the following, nothing returns



      myfun(data, group, age, bmi)


      Ideally, I would like to group both age and bmi by group and return the mean and sd for each. In the future, I would like to pass many more columns from data into the function...



      The output would be similar to summaryBy from doby package, but on many columns at once and would look like:



      Group   age.mean    age.sd
      0
      1
      bmi.mean bmi.sd
      0
      1






      r function for-loop dplyr summarytools






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 13:47









      mdb_ftlmdb_ftl

      52




      52
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Your loop appears to be unnecessary (you aren't doing anything with i). Instead, you could use summarize_at to achieve the effect you want:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd))

          }


          And if we test it out with diamonds dataset:



          myfun(diamonds, cut, x, z)

          cut x_mean z_mean x_sd z_sd
          <ord> <dbl> <dbl> <dbl> <dbl>
          1 Fair 6.25 3.98 0.964 0.652
          2 Good 5.84 3.64 1.06 0.655
          3 Very Good 5.74 3.56 1.10 0.730
          4 Premium 5.97 3.65 1.19 0.731
          5 Ideal 5.51 3.40 1.06 0.658


          To get the formatting closer to what you had in mind in your original post, we can use a bit of tidyr magic:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd)) %>%
          gather(variable, value, -(!!var1)) %>%
          separate(variable, c('variable', 'measure'), sep = '_') %>%
          spread(measure, value) %>%
          arrange(variable, !!var1)

          }

          cut variable mean sd
          <ord> <chr> <dbl> <dbl>
          1 Fair x 6.25 0.964
          2 Good x 5.84 1.06
          3 Very Good x 5.74 1.10
          4 Premium x 5.97 1.19
          5 Ideal x 5.51 1.06
          6 Fair z 3.98 0.652
          7 Good z 3.64 0.655
          8 Very Good z 3.56 0.730
          9 Premium z 3.65 0.731
          10 Ideal z 3.40 0.658





          share|improve this answer


























          • This solution worked well for me. Can you explain how I might output the mean and sd by group for each column into a format which is similar to the original post? (more vertically aligned?, perhaps each result is outputted as a matrix?). If I input many more columns (x, y, z, a, b, c, etc...) the result is going to very difficult to read horizontally

            – mdb_ftl
            Nov 17 '18 at 14:14











          • See updated answer.

            – jdobres
            Nov 17 '18 at 14:28











          • This is precisely what I was looking for and works very well. I learned a lot from this answer.

            – mdb_ftl
            Nov 17 '18 at 14:48











          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%2f53351824%2fpass-multiple-columns-in-dataframe-into-function-at-once-in-r%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









          2














          Your loop appears to be unnecessary (you aren't doing anything with i). Instead, you could use summarize_at to achieve the effect you want:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd))

          }


          And if we test it out with diamonds dataset:



          myfun(diamonds, cut, x, z)

          cut x_mean z_mean x_sd z_sd
          <ord> <dbl> <dbl> <dbl> <dbl>
          1 Fair 6.25 3.98 0.964 0.652
          2 Good 5.84 3.64 1.06 0.655
          3 Very Good 5.74 3.56 1.10 0.730
          4 Premium 5.97 3.65 1.19 0.731
          5 Ideal 5.51 3.40 1.06 0.658


          To get the formatting closer to what you had in mind in your original post, we can use a bit of tidyr magic:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd)) %>%
          gather(variable, value, -(!!var1)) %>%
          separate(variable, c('variable', 'measure'), sep = '_') %>%
          spread(measure, value) %>%
          arrange(variable, !!var1)

          }

          cut variable mean sd
          <ord> <chr> <dbl> <dbl>
          1 Fair x 6.25 0.964
          2 Good x 5.84 1.06
          3 Very Good x 5.74 1.10
          4 Premium x 5.97 1.19
          5 Ideal x 5.51 1.06
          6 Fair z 3.98 0.652
          7 Good z 3.64 0.655
          8 Very Good z 3.56 0.730
          9 Premium z 3.65 0.731
          10 Ideal z 3.40 0.658





          share|improve this answer


























          • This solution worked well for me. Can you explain how I might output the mean and sd by group for each column into a format which is similar to the original post? (more vertically aligned?, perhaps each result is outputted as a matrix?). If I input many more columns (x, y, z, a, b, c, etc...) the result is going to very difficult to read horizontally

            – mdb_ftl
            Nov 17 '18 at 14:14











          • See updated answer.

            – jdobres
            Nov 17 '18 at 14:28











          • This is precisely what I was looking for and works very well. I learned a lot from this answer.

            – mdb_ftl
            Nov 17 '18 at 14:48
















          2














          Your loop appears to be unnecessary (you aren't doing anything with i). Instead, you could use summarize_at to achieve the effect you want:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd))

          }


          And if we test it out with diamonds dataset:



          myfun(diamonds, cut, x, z)

          cut x_mean z_mean x_sd z_sd
          <ord> <dbl> <dbl> <dbl> <dbl>
          1 Fair 6.25 3.98 0.964 0.652
          2 Good 5.84 3.64 1.06 0.655
          3 Very Good 5.74 3.56 1.10 0.730
          4 Premium 5.97 3.65 1.19 0.731
          5 Ideal 5.51 3.40 1.06 0.658


          To get the formatting closer to what you had in mind in your original post, we can use a bit of tidyr magic:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd)) %>%
          gather(variable, value, -(!!var1)) %>%
          separate(variable, c('variable', 'measure'), sep = '_') %>%
          spread(measure, value) %>%
          arrange(variable, !!var1)

          }

          cut variable mean sd
          <ord> <chr> <dbl> <dbl>
          1 Fair x 6.25 0.964
          2 Good x 5.84 1.06
          3 Very Good x 5.74 1.10
          4 Premium x 5.97 1.19
          5 Ideal x 5.51 1.06
          6 Fair z 3.98 0.652
          7 Good z 3.64 0.655
          8 Very Good z 3.56 0.730
          9 Premium z 3.65 0.731
          10 Ideal z 3.40 0.658





          share|improve this answer


























          • This solution worked well for me. Can you explain how I might output the mean and sd by group for each column into a format which is similar to the original post? (more vertically aligned?, perhaps each result is outputted as a matrix?). If I input many more columns (x, y, z, a, b, c, etc...) the result is going to very difficult to read horizontally

            – mdb_ftl
            Nov 17 '18 at 14:14











          • See updated answer.

            – jdobres
            Nov 17 '18 at 14:28











          • This is precisely what I was looking for and works very well. I learned a lot from this answer.

            – mdb_ftl
            Nov 17 '18 at 14:48














          2












          2








          2







          Your loop appears to be unnecessary (you aren't doing anything with i). Instead, you could use summarize_at to achieve the effect you want:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd))

          }


          And if we test it out with diamonds dataset:



          myfun(diamonds, cut, x, z)

          cut x_mean z_mean x_sd z_sd
          <ord> <dbl> <dbl> <dbl> <dbl>
          1 Fair 6.25 3.98 0.964 0.652
          2 Good 5.84 3.64 1.06 0.655
          3 Very Good 5.74 3.56 1.10 0.730
          4 Premium 5.97 3.65 1.19 0.731
          5 Ideal 5.51 3.40 1.06 0.658


          To get the formatting closer to what you had in mind in your original post, we can use a bit of tidyr magic:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd)) %>%
          gather(variable, value, -(!!var1)) %>%
          separate(variable, c('variable', 'measure'), sep = '_') %>%
          spread(measure, value) %>%
          arrange(variable, !!var1)

          }

          cut variable mean sd
          <ord> <chr> <dbl> <dbl>
          1 Fair x 6.25 0.964
          2 Good x 5.84 1.06
          3 Very Good x 5.74 1.10
          4 Premium x 5.97 1.19
          5 Ideal x 5.51 1.06
          6 Fair z 3.98 0.652
          7 Good z 3.64 0.655
          8 Very Good z 3.56 0.730
          9 Premium z 3.65 0.731
          10 Ideal z 3.40 0.658





          share|improve this answer















          Your loop appears to be unnecessary (you aren't doing anything with i). Instead, you could use summarize_at to achieve the effect you want:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd))

          }


          And if we test it out with diamonds dataset:



          myfun(diamonds, cut, x, z)

          cut x_mean z_mean x_sd z_sd
          <ord> <dbl> <dbl> <dbl> <dbl>
          1 Fair 6.25 3.98 0.964 0.652
          2 Good 5.84 3.64 1.06 0.655
          3 Very Good 5.74 3.56 1.10 0.730
          4 Premium 5.97 3.65 1.19 0.731
          5 Ideal 5.51 3.40 1.06 0.658


          To get the formatting closer to what you had in mind in your original post, we can use a bit of tidyr magic:



          myfun <- function(db,var1, ...) {

          var1 <- enquo(var1)
          var2 <- quos(...)

          db %>%
          group_by(!!var1) %>%
          summarise_at(vars(!!!var2), c(mean = mean, sd = sd)) %>%
          gather(variable, value, -(!!var1)) %>%
          separate(variable, c('variable', 'measure'), sep = '_') %>%
          spread(measure, value) %>%
          arrange(variable, !!var1)

          }

          cut variable mean sd
          <ord> <chr> <dbl> <dbl>
          1 Fair x 6.25 0.964
          2 Good x 5.84 1.06
          3 Very Good x 5.74 1.10
          4 Premium x 5.97 1.19
          5 Ideal x 5.51 1.06
          6 Fair z 3.98 0.652
          7 Good z 3.64 0.655
          8 Very Good z 3.56 0.730
          9 Premium z 3.65 0.731
          10 Ideal z 3.40 0.658






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 '18 at 14:28

























          answered Nov 17 '18 at 14:05









          jdobresjdobres

          4,8561522




          4,8561522













          • This solution worked well for me. Can you explain how I might output the mean and sd by group for each column into a format which is similar to the original post? (more vertically aligned?, perhaps each result is outputted as a matrix?). If I input many more columns (x, y, z, a, b, c, etc...) the result is going to very difficult to read horizontally

            – mdb_ftl
            Nov 17 '18 at 14:14











          • See updated answer.

            – jdobres
            Nov 17 '18 at 14:28











          • This is precisely what I was looking for and works very well. I learned a lot from this answer.

            – mdb_ftl
            Nov 17 '18 at 14:48



















          • This solution worked well for me. Can you explain how I might output the mean and sd by group for each column into a format which is similar to the original post? (more vertically aligned?, perhaps each result is outputted as a matrix?). If I input many more columns (x, y, z, a, b, c, etc...) the result is going to very difficult to read horizontally

            – mdb_ftl
            Nov 17 '18 at 14:14











          • See updated answer.

            – jdobres
            Nov 17 '18 at 14:28











          • This is precisely what I was looking for and works very well. I learned a lot from this answer.

            – mdb_ftl
            Nov 17 '18 at 14:48

















          This solution worked well for me. Can you explain how I might output the mean and sd by group for each column into a format which is similar to the original post? (more vertically aligned?, perhaps each result is outputted as a matrix?). If I input many more columns (x, y, z, a, b, c, etc...) the result is going to very difficult to read horizontally

          – mdb_ftl
          Nov 17 '18 at 14:14





          This solution worked well for me. Can you explain how I might output the mean and sd by group for each column into a format which is similar to the original post? (more vertically aligned?, perhaps each result is outputted as a matrix?). If I input many more columns (x, y, z, a, b, c, etc...) the result is going to very difficult to read horizontally

          – mdb_ftl
          Nov 17 '18 at 14:14













          See updated answer.

          – jdobres
          Nov 17 '18 at 14:28





          See updated answer.

          – jdobres
          Nov 17 '18 at 14:28













          This is precisely what I was looking for and works very well. I learned a lot from this answer.

          – mdb_ftl
          Nov 17 '18 at 14:48





          This is precisely what I was looking for and works very well. I learned a lot from this answer.

          – mdb_ftl
          Nov 17 '18 at 14:48


















          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%2f53351824%2fpass-multiple-columns-in-dataframe-into-function-at-once-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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()