passing data.frame column names to a function












1















I've seen similar posts that mention the need for use of quotes to pass column names to functions, but I could use help in what I have wrong and how I could improve the function. For example, maybe I could add a "suffix" argument to automatically assign the new data.frame with a suffixed name of the original? I'm hoping to be able to have a generic function that I could use for varying column names and positions. Thank you.



library(tidyverse)  

# function definition #
createhrly_0595quants <- function(df, hourcolumn,
value, qtype, metadata_to_add) {

df <- df %>% group_by(hourcolumn) %>%
summarize(`05%`=quantile(value, probs=0.05, type =qtype),
`95%`=quantile(value, probs=0.95, type = qtype),
median = median(value), n=n()) %>%
mutate(qtype = qtype, metadata_to_add = metadata_to_add)

}

# sample data.frame #
hrly_gmt <- seq(from=as.POSIXct("2018-11-20 01:00",
tz="America/Los_Angeles"), to=as.POSIXct("2018-11-20 23:00",
tz="America/Los_Angeles"), by="1 hours")
myvalues1 <- rnorm(23)
myvalues2 <- rnorm(23)
mydf1 <- data.frame(hrly_gmt, myvalues1) %>% mutate(class = "a")
mydf2 <- data.frame(hrly_gmt, myvalues2) %>% mutate(class = "b")
df_x <- rbind(mydf1, mydf2)

# function use #
df_0595quants <- createhrly_0595quants(df_x, "hrly_gmt",
"myvalues", 4, "version x.2")









share|improve this question





























    1















    I've seen similar posts that mention the need for use of quotes to pass column names to functions, but I could use help in what I have wrong and how I could improve the function. For example, maybe I could add a "suffix" argument to automatically assign the new data.frame with a suffixed name of the original? I'm hoping to be able to have a generic function that I could use for varying column names and positions. Thank you.



    library(tidyverse)  

    # function definition #
    createhrly_0595quants <- function(df, hourcolumn,
    value, qtype, metadata_to_add) {

    df <- df %>% group_by(hourcolumn) %>%
    summarize(`05%`=quantile(value, probs=0.05, type =qtype),
    `95%`=quantile(value, probs=0.95, type = qtype),
    median = median(value), n=n()) %>%
    mutate(qtype = qtype, metadata_to_add = metadata_to_add)

    }

    # sample data.frame #
    hrly_gmt <- seq(from=as.POSIXct("2018-11-20 01:00",
    tz="America/Los_Angeles"), to=as.POSIXct("2018-11-20 23:00",
    tz="America/Los_Angeles"), by="1 hours")
    myvalues1 <- rnorm(23)
    myvalues2 <- rnorm(23)
    mydf1 <- data.frame(hrly_gmt, myvalues1) %>% mutate(class = "a")
    mydf2 <- data.frame(hrly_gmt, myvalues2) %>% mutate(class = "b")
    df_x <- rbind(mydf1, mydf2)

    # function use #
    df_0595quants <- createhrly_0595quants(df_x, "hrly_gmt",
    "myvalues", 4, "version x.2")









    share|improve this question



























      1












      1








      1








      I've seen similar posts that mention the need for use of quotes to pass column names to functions, but I could use help in what I have wrong and how I could improve the function. For example, maybe I could add a "suffix" argument to automatically assign the new data.frame with a suffixed name of the original? I'm hoping to be able to have a generic function that I could use for varying column names and positions. Thank you.



      library(tidyverse)  

      # function definition #
      createhrly_0595quants <- function(df, hourcolumn,
      value, qtype, metadata_to_add) {

      df <- df %>% group_by(hourcolumn) %>%
      summarize(`05%`=quantile(value, probs=0.05, type =qtype),
      `95%`=quantile(value, probs=0.95, type = qtype),
      median = median(value), n=n()) %>%
      mutate(qtype = qtype, metadata_to_add = metadata_to_add)

      }

      # sample data.frame #
      hrly_gmt <- seq(from=as.POSIXct("2018-11-20 01:00",
      tz="America/Los_Angeles"), to=as.POSIXct("2018-11-20 23:00",
      tz="America/Los_Angeles"), by="1 hours")
      myvalues1 <- rnorm(23)
      myvalues2 <- rnorm(23)
      mydf1 <- data.frame(hrly_gmt, myvalues1) %>% mutate(class = "a")
      mydf2 <- data.frame(hrly_gmt, myvalues2) %>% mutate(class = "b")
      df_x <- rbind(mydf1, mydf2)

      # function use #
      df_0595quants <- createhrly_0595quants(df_x, "hrly_gmt",
      "myvalues", 4, "version x.2")









      share|improve this question
















      I've seen similar posts that mention the need for use of quotes to pass column names to functions, but I could use help in what I have wrong and how I could improve the function. For example, maybe I could add a "suffix" argument to automatically assign the new data.frame with a suffixed name of the original? I'm hoping to be able to have a generic function that I could use for varying column names and positions. Thank you.



      library(tidyverse)  

      # function definition #
      createhrly_0595quants <- function(df, hourcolumn,
      value, qtype, metadata_to_add) {

      df <- df %>% group_by(hourcolumn) %>%
      summarize(`05%`=quantile(value, probs=0.05, type =qtype),
      `95%`=quantile(value, probs=0.95, type = qtype),
      median = median(value), n=n()) %>%
      mutate(qtype = qtype, metadata_to_add = metadata_to_add)

      }

      # sample data.frame #
      hrly_gmt <- seq(from=as.POSIXct("2018-11-20 01:00",
      tz="America/Los_Angeles"), to=as.POSIXct("2018-11-20 23:00",
      tz="America/Los_Angeles"), by="1 hours")
      myvalues1 <- rnorm(23)
      myvalues2 <- rnorm(23)
      mydf1 <- data.frame(hrly_gmt, myvalues1) %>% mutate(class = "a")
      mydf2 <- data.frame(hrly_gmt, myvalues2) %>% mutate(class = "b")
      df_x <- rbind(mydf1, mydf2)

      # function use #
      df_0595quants <- createhrly_0595quants(df_x, "hrly_gmt",
      "myvalues", 4, "version x.2")






      r tidyverse






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 18:58







      doconnor

















      asked Nov 20 '18 at 18:41









      doconnordoconnor

      322111




      322111
























          1 Answer
          1






          active

          oldest

          votes


















          2














          As we pass strings as input, instead of using group_by, we can use group_by_at which takes strings for column names, the summarize column can be converted to symbol (sym) and evaluate (!!)



          createhrly_0595quants <- function(df, hourcolumn, 
          value, qtype, metadata_to_add) {

          value <- rlang::sym(value)


          df %>%
          group_by_at(vars(hourcolumn)) %>%
          summarize(`05%`=quantile(!!value, probs=0.05, type =qtype),
          `95%`=quantile(!!value, probs=0.95, type = qtype),
          median = median(!!value), n=n()) %>%
          mutate(qtype = qtype,
          metadata_to_add = metadata_to_add)
          }



          createhrly_0595quants(df_x, "hrly_gmt",
          "myvalues", 4, "version x.2")





          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%2f53399489%2fpassing-data-frame-column-names-to-a-function%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














            As we pass strings as input, instead of using group_by, we can use group_by_at which takes strings for column names, the summarize column can be converted to symbol (sym) and evaluate (!!)



            createhrly_0595quants <- function(df, hourcolumn, 
            value, qtype, metadata_to_add) {

            value <- rlang::sym(value)


            df %>%
            group_by_at(vars(hourcolumn)) %>%
            summarize(`05%`=quantile(!!value, probs=0.05, type =qtype),
            `95%`=quantile(!!value, probs=0.95, type = qtype),
            median = median(!!value), n=n()) %>%
            mutate(qtype = qtype,
            metadata_to_add = metadata_to_add)
            }



            createhrly_0595quants(df_x, "hrly_gmt",
            "myvalues", 4, "version x.2")





            share|improve this answer




























              2














              As we pass strings as input, instead of using group_by, we can use group_by_at which takes strings for column names, the summarize column can be converted to symbol (sym) and evaluate (!!)



              createhrly_0595quants <- function(df, hourcolumn, 
              value, qtype, metadata_to_add) {

              value <- rlang::sym(value)


              df %>%
              group_by_at(vars(hourcolumn)) %>%
              summarize(`05%`=quantile(!!value, probs=0.05, type =qtype),
              `95%`=quantile(!!value, probs=0.95, type = qtype),
              median = median(!!value), n=n()) %>%
              mutate(qtype = qtype,
              metadata_to_add = metadata_to_add)
              }



              createhrly_0595quants(df_x, "hrly_gmt",
              "myvalues", 4, "version x.2")





              share|improve this answer


























                2












                2








                2







                As we pass strings as input, instead of using group_by, we can use group_by_at which takes strings for column names, the summarize column can be converted to symbol (sym) and evaluate (!!)



                createhrly_0595quants <- function(df, hourcolumn, 
                value, qtype, metadata_to_add) {

                value <- rlang::sym(value)


                df %>%
                group_by_at(vars(hourcolumn)) %>%
                summarize(`05%`=quantile(!!value, probs=0.05, type =qtype),
                `95%`=quantile(!!value, probs=0.95, type = qtype),
                median = median(!!value), n=n()) %>%
                mutate(qtype = qtype,
                metadata_to_add = metadata_to_add)
                }



                createhrly_0595quants(df_x, "hrly_gmt",
                "myvalues", 4, "version x.2")





                share|improve this answer













                As we pass strings as input, instead of using group_by, we can use group_by_at which takes strings for column names, the summarize column can be converted to symbol (sym) and evaluate (!!)



                createhrly_0595quants <- function(df, hourcolumn, 
                value, qtype, metadata_to_add) {

                value <- rlang::sym(value)


                df %>%
                group_by_at(vars(hourcolumn)) %>%
                summarize(`05%`=quantile(!!value, probs=0.05, type =qtype),
                `95%`=quantile(!!value, probs=0.95, type = qtype),
                median = median(!!value), n=n()) %>%
                mutate(qtype = qtype,
                metadata_to_add = metadata_to_add)
                }



                createhrly_0595quants(df_x, "hrly_gmt",
                "myvalues", 4, "version x.2")






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 18:44









                akrunakrun

                410k13198273




                410k13198273
































                    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%2f53399489%2fpassing-data-frame-column-names-to-a-function%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 send String Array data to Server using php in android

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Is anime1.com a legal site for watching anime?