tidyeval difference between mutate `:=` and mutate `=`












3















Both these code blocks work even though they use different equal signs, one with := and the other with =. Which is correct and why? I thought tidyeval required := when using dplyr functions, but strange enough = works just fine in my mutate call.



1



library(tidyverse)
set.seed(1)
graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
random_num = rnorm(30, 8, 5))

child_function <- function(df, variable, hor.line = 6) {
variable <- enquo(variable)
df <- mutate(df, mutation := 2 * !! variable, horizontal.line := hor.line)
df
}

child_function(graph.data, variable = random_num, hor.line=8)


2



library(tidyverse)
set.seed(1)
graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
random_num = rnorm(30, 8, 5))

child_function <- function(df, variable, hor.line = 6) {
variable <- enquo(variable)
df <- mutate(df, mutation = 2 * !! variable, horizontal.line = hor.line)
df
}

child_function(graph.data, variable = random_num, hor.line=8)









share|improve this question



























    3















    Both these code blocks work even though they use different equal signs, one with := and the other with =. Which is correct and why? I thought tidyeval required := when using dplyr functions, but strange enough = works just fine in my mutate call.



    1



    library(tidyverse)
    set.seed(1)
    graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
    random_num = rnorm(30, 8, 5))

    child_function <- function(df, variable, hor.line = 6) {
    variable <- enquo(variable)
    df <- mutate(df, mutation := 2 * !! variable, horizontal.line := hor.line)
    df
    }

    child_function(graph.data, variable = random_num, hor.line=8)


    2



    library(tidyverse)
    set.seed(1)
    graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
    random_num = rnorm(30, 8, 5))

    child_function <- function(df, variable, hor.line = 6) {
    variable <- enquo(variable)
    df <- mutate(df, mutation = 2 * !! variable, horizontal.line = hor.line)
    df
    }

    child_function(graph.data, variable = random_num, hor.line=8)









    share|improve this question

























      3












      3








      3








      Both these code blocks work even though they use different equal signs, one with := and the other with =. Which is correct and why? I thought tidyeval required := when using dplyr functions, but strange enough = works just fine in my mutate call.



      1



      library(tidyverse)
      set.seed(1)
      graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
      random_num = rnorm(30, 8, 5))

      child_function <- function(df, variable, hor.line = 6) {
      variable <- enquo(variable)
      df <- mutate(df, mutation := 2 * !! variable, horizontal.line := hor.line)
      df
      }

      child_function(graph.data, variable = random_num, hor.line=8)


      2



      library(tidyverse)
      set.seed(1)
      graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
      random_num = rnorm(30, 8, 5))

      child_function <- function(df, variable, hor.line = 6) {
      variable <- enquo(variable)
      df <- mutate(df, mutation = 2 * !! variable, horizontal.line = hor.line)
      df
      }

      child_function(graph.data, variable = random_num, hor.line=8)









      share|improve this question














      Both these code blocks work even though they use different equal signs, one with := and the other with =. Which is correct and why? I thought tidyeval required := when using dplyr functions, but strange enough = works just fine in my mutate call.



      1



      library(tidyverse)
      set.seed(1)
      graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
      random_num = rnorm(30, 8, 5))

      child_function <- function(df, variable, hor.line = 6) {
      variable <- enquo(variable)
      df <- mutate(df, mutation := 2 * !! variable, horizontal.line := hor.line)
      df
      }

      child_function(graph.data, variable = random_num, hor.line=8)


      2



      library(tidyverse)
      set.seed(1)
      graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
      random_num = rnorm(30, 8, 5))

      child_function <- function(df, variable, hor.line = 6) {
      variable <- enquo(variable)
      df <- mutate(df, mutation = 2 * !! variable, horizontal.line = hor.line)
      df
      }

      child_function(graph.data, variable = random_num, hor.line=8)






      r function dplyr






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 18:14









      stackinatorstackinator

      1,219419




      1,219419
























          3 Answers
          3






          active

          oldest

          votes


















          2














          There is no obligation to put := in that case.



          It becomes obligatory when you want to do something like:



          child_function <- function(df, variable, hor.line = 6, mt_name = "mutation") {  

          variable <- enquo(variable)

          df <- mutate(df, !! mt_name := 2 * !! variable, horizontal.line = hor.line)

          }





          share|improve this answer

































            4














            The := operator's purpose is to allow you to dynamically set the name of variable on the LHS (left hand side) of the equation, which you are not doing here.



            In many cases, including this one, we're just concerned with manipulating the RHS. The := would come in handy if you wanted to control the name of the "mutation" variable.



            https://dplyr.tidyverse.org/articles/programming.html#setting-variable-names






            share|improve this answer































              2














              A little bit hard to track down, but from ?quasiquotation




              Unfortunately R is very strict about the kind of expressions supported
              on the LHS of =. This is why we have made the more flexible :=
              operator an alias of =. You can use it to supply names, e.g. a := b is
              equivalent to a = b. Since its syntax is more flexible you can unquote
              on the LHS:







              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%2f53267860%2ftidyeval-difference-between-mutate-and-mutate%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









                2














                There is no obligation to put := in that case.



                It becomes obligatory when you want to do something like:



                child_function <- function(df, variable, hor.line = 6, mt_name = "mutation") {  

                variable <- enquo(variable)

                df <- mutate(df, !! mt_name := 2 * !! variable, horizontal.line = hor.line)

                }





                share|improve this answer






























                  2














                  There is no obligation to put := in that case.



                  It becomes obligatory when you want to do something like:



                  child_function <- function(df, variable, hor.line = 6, mt_name = "mutation") {  

                  variable <- enquo(variable)

                  df <- mutate(df, !! mt_name := 2 * !! variable, horizontal.line = hor.line)

                  }





                  share|improve this answer




























                    2












                    2








                    2







                    There is no obligation to put := in that case.



                    It becomes obligatory when you want to do something like:



                    child_function <- function(df, variable, hor.line = 6, mt_name = "mutation") {  

                    variable <- enquo(variable)

                    df <- mutate(df, !! mt_name := 2 * !! variable, horizontal.line = hor.line)

                    }





                    share|improve this answer















                    There is no obligation to put := in that case.



                    It becomes obligatory when you want to do something like:



                    child_function <- function(df, variable, hor.line = 6, mt_name = "mutation") {  

                    variable <- enquo(variable)

                    df <- mutate(df, !! mt_name := 2 * !! variable, horizontal.line = hor.line)

                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 12 '18 at 18:34

























                    answered Nov 12 '18 at 18:29









                    arg0nautarg0naut

                    2,142314




                    2,142314

























                        4














                        The := operator's purpose is to allow you to dynamically set the name of variable on the LHS (left hand side) of the equation, which you are not doing here.



                        In many cases, including this one, we're just concerned with manipulating the RHS. The := would come in handy if you wanted to control the name of the "mutation" variable.



                        https://dplyr.tidyverse.org/articles/programming.html#setting-variable-names






                        share|improve this answer




























                          4














                          The := operator's purpose is to allow you to dynamically set the name of variable on the LHS (left hand side) of the equation, which you are not doing here.



                          In many cases, including this one, we're just concerned with manipulating the RHS. The := would come in handy if you wanted to control the name of the "mutation" variable.



                          https://dplyr.tidyverse.org/articles/programming.html#setting-variable-names






                          share|improve this answer


























                            4












                            4








                            4







                            The := operator's purpose is to allow you to dynamically set the name of variable on the LHS (left hand side) of the equation, which you are not doing here.



                            In many cases, including this one, we're just concerned with manipulating the RHS. The := would come in handy if you wanted to control the name of the "mutation" variable.



                            https://dplyr.tidyverse.org/articles/programming.html#setting-variable-names






                            share|improve this answer













                            The := operator's purpose is to allow you to dynamically set the name of variable on the LHS (left hand side) of the equation, which you are not doing here.



                            In many cases, including this one, we're just concerned with manipulating the RHS. The := would come in handy if you wanted to control the name of the "mutation" variable.



                            https://dplyr.tidyverse.org/articles/programming.html#setting-variable-names







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 '18 at 18:30









                            Jon SpringJon Spring

                            5,4131625




                            5,4131625























                                2














                                A little bit hard to track down, but from ?quasiquotation




                                Unfortunately R is very strict about the kind of expressions supported
                                on the LHS of =. This is why we have made the more flexible :=
                                operator an alias of =. You can use it to supply names, e.g. a := b is
                                equivalent to a = b. Since its syntax is more flexible you can unquote
                                on the LHS:







                                share|improve this answer




























                                  2














                                  A little bit hard to track down, but from ?quasiquotation




                                  Unfortunately R is very strict about the kind of expressions supported
                                  on the LHS of =. This is why we have made the more flexible :=
                                  operator an alias of =. You can use it to supply names, e.g. a := b is
                                  equivalent to a = b. Since its syntax is more flexible you can unquote
                                  on the LHS:







                                  share|improve this answer


























                                    2












                                    2








                                    2







                                    A little bit hard to track down, but from ?quasiquotation




                                    Unfortunately R is very strict about the kind of expressions supported
                                    on the LHS of =. This is why we have made the more flexible :=
                                    operator an alias of =. You can use it to supply names, e.g. a := b is
                                    equivalent to a = b. Since its syntax is more flexible you can unquote
                                    on the LHS:







                                    share|improve this answer













                                    A little bit hard to track down, but from ?quasiquotation




                                    Unfortunately R is very strict about the kind of expressions supported
                                    on the LHS of =. This is why we have made the more flexible :=
                                    operator an alias of =. You can use it to supply names, e.g. a := b is
                                    equivalent to a = b. Since its syntax is more flexible you can unquote
                                    on the LHS:








                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 12 '18 at 18:34









                                    dwwdww

                                    14.6k22655




                                    14.6k22655






























                                        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%2f53267860%2ftidyeval-difference-between-mutate-and-mutate%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

                                        Full-time equivalent

                                        Bicuculline

                                        さくらももこ