tidyeval difference between mutate `:=` and mutate `=`
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
add a comment |
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
add a comment |
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
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
r function dplyr
asked Nov 12 '18 at 18:14
stackinatorstackinator
1,219419
1,219419
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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)
}
add a comment |
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
add a comment |
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:
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
}
add a comment |
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)
}
add a comment |
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)
}
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)
}
edited Nov 12 '18 at 18:34
answered Nov 12 '18 at 18:29
arg0nautarg0naut
2,142314
2,142314
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 12 '18 at 18:30
Jon SpringJon Spring
5,4131625
5,4131625
add a comment |
add a comment |
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:
add a comment |
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:
add a comment |
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:
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:
answered Nov 12 '18 at 18:34
dwwdww
14.6k22655
14.6k22655
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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