DRY when enumerating variables in R for multiple operations in a pipeline
I'm wondering if there is a DRY way to write the following pipe:
library(tidyverse)
data(iris)
iris %>% arrange(Sepal.Width, Species) %>% select(Sepal.Width, Species)
This works perfectly but if a change in the code is needed, I have two places to edit.
Is there any way to rewrite the code in such a way that the variables are listed only once in the pipeline?
I'd hope there is a way I can store the variable list v
and then call:
iris %>% arrange(v) %>% select(v)
I've tried to use quote
, Sym
, and many other functions of Non Standard Evaluation in order to store the list of variables to no avail.
All those answers were unhelpful for this problem:
r - how to use a variable in a variable
Using a variable to refer to another variable in R?
R expression variable list
r - how to use a variable in a variable
r coding-style dry tidyverse
add a comment |
I'm wondering if there is a DRY way to write the following pipe:
library(tidyverse)
data(iris)
iris %>% arrange(Sepal.Width, Species) %>% select(Sepal.Width, Species)
This works perfectly but if a change in the code is needed, I have two places to edit.
Is there any way to rewrite the code in such a way that the variables are listed only once in the pipeline?
I'd hope there is a way I can store the variable list v
and then call:
iris %>% arrange(v) %>% select(v)
I've tried to use quote
, Sym
, and many other functions of Non Standard Evaluation in order to store the list of variables to no avail.
All those answers were unhelpful for this problem:
r - how to use a variable in a variable
Using a variable to refer to another variable in R?
R expression variable list
r - how to use a variable in a variable
r coding-style dry tidyverse
There is a big difference between DRY and what you're talking about.
– hrbrmstr
Nov 24 '18 at 2:30
2
Possible duplicate of Pass a vector of variable names to arrange() in dplyr
– Len Greski
Nov 24 '18 at 3:30
Yes, that solved my problem. I think that question should reworded more generally so that It can be found quickly.
– xihh
Nov 24 '18 at 18:46
add a comment |
I'm wondering if there is a DRY way to write the following pipe:
library(tidyverse)
data(iris)
iris %>% arrange(Sepal.Width, Species) %>% select(Sepal.Width, Species)
This works perfectly but if a change in the code is needed, I have two places to edit.
Is there any way to rewrite the code in such a way that the variables are listed only once in the pipeline?
I'd hope there is a way I can store the variable list v
and then call:
iris %>% arrange(v) %>% select(v)
I've tried to use quote
, Sym
, and many other functions of Non Standard Evaluation in order to store the list of variables to no avail.
All those answers were unhelpful for this problem:
r - how to use a variable in a variable
Using a variable to refer to another variable in R?
R expression variable list
r - how to use a variable in a variable
r coding-style dry tidyverse
I'm wondering if there is a DRY way to write the following pipe:
library(tidyverse)
data(iris)
iris %>% arrange(Sepal.Width, Species) %>% select(Sepal.Width, Species)
This works perfectly but if a change in the code is needed, I have two places to edit.
Is there any way to rewrite the code in such a way that the variables are listed only once in the pipeline?
I'd hope there is a way I can store the variable list v
and then call:
iris %>% arrange(v) %>% select(v)
I've tried to use quote
, Sym
, and many other functions of Non Standard Evaluation in order to store the list of variables to no avail.
All those answers were unhelpful for this problem:
r - how to use a variable in a variable
Using a variable to refer to another variable in R?
R expression variable list
r - how to use a variable in a variable
r coding-style dry tidyverse
r coding-style dry tidyverse
edited Nov 24 '18 at 18:43
xihh
asked Nov 23 '18 at 23:55
xihhxihh
515
515
There is a big difference between DRY and what you're talking about.
– hrbrmstr
Nov 24 '18 at 2:30
2
Possible duplicate of Pass a vector of variable names to arrange() in dplyr
– Len Greski
Nov 24 '18 at 3:30
Yes, that solved my problem. I think that question should reworded more generally so that It can be found quickly.
– xihh
Nov 24 '18 at 18:46
add a comment |
There is a big difference between DRY and what you're talking about.
– hrbrmstr
Nov 24 '18 at 2:30
2
Possible duplicate of Pass a vector of variable names to arrange() in dplyr
– Len Greski
Nov 24 '18 at 3:30
Yes, that solved my problem. I think that question should reworded more generally so that It can be found quickly.
– xihh
Nov 24 '18 at 18:46
There is a big difference between DRY and what you're talking about.
– hrbrmstr
Nov 24 '18 at 2:30
There is a big difference between DRY and what you're talking about.
– hrbrmstr
Nov 24 '18 at 2:30
2
2
Possible duplicate of Pass a vector of variable names to arrange() in dplyr
– Len Greski
Nov 24 '18 at 3:30
Possible duplicate of Pass a vector of variable names to arrange() in dplyr
– Len Greski
Nov 24 '18 at 3:30
Yes, that solved my problem. I think that question should reworded more generally so that It can be found quickly.
– xihh
Nov 24 '18 at 18:46
Yes, that solved my problem. I think that question should reworded more generally so that It can be found quickly.
– xihh
Nov 24 '18 at 18:46
add a comment |
2 Answers
2
active
oldest
votes
I think what you looking for is:
library(tidyverse)
vars <- quos(Sepal.Width, Species)
iris %>% arrange(!!!vars) %>% select(!!!vars)
I assumed you meaned select
rather than filter
as your question stated since iris %>% arrange(Sepal.Width, Species) %>% filter(Sepal.Width, Species)
throws an error
You are right, select. I'll edit my post.
– xihh
Nov 24 '18 at 18:42
add a comment |
Yes, it's a duplicate of Pass a vector of variable names to arrange() in dplyr...
library(tidyverse)
data(iris)
varList <- c("Sepal.Width","Species")
iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
...and the output:
> iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
Sepal.Width Species
1 2.0 versicolor
2 2.2 versicolor
3 2.2 versicolor
4 2.2 virginica
5 2.3 setosa
6 2.3 versicolor
7 2.3 versicolor
8 2.3 versicolor
9 2.4 versicolor
10 2.4 versicolor
11 2.4 versicolor
12 2.5 versicolor
13 2.5 versicolor
14 2.5 versicolor
15 2.5 versicolor
16 2.5 virginica
17 2.5 virginica
18 2.5 virginica
19 2.5 virginica
20 2.6 versicolor
21 2.6 versicolor
22 2.6 versicolor
23 2.6 virginica
24 2.6 virginica
...
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%2f53454013%2fdry-when-enumerating-variables-in-r-for-multiple-operations-in-a-pipeline%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
I think what you looking for is:
library(tidyverse)
vars <- quos(Sepal.Width, Species)
iris %>% arrange(!!!vars) %>% select(!!!vars)
I assumed you meaned select
rather than filter
as your question stated since iris %>% arrange(Sepal.Width, Species) %>% filter(Sepal.Width, Species)
throws an error
You are right, select. I'll edit my post.
– xihh
Nov 24 '18 at 18:42
add a comment |
I think what you looking for is:
library(tidyverse)
vars <- quos(Sepal.Width, Species)
iris %>% arrange(!!!vars) %>% select(!!!vars)
I assumed you meaned select
rather than filter
as your question stated since iris %>% arrange(Sepal.Width, Species) %>% filter(Sepal.Width, Species)
throws an error
You are right, select. I'll edit my post.
– xihh
Nov 24 '18 at 18:42
add a comment |
I think what you looking for is:
library(tidyverse)
vars <- quos(Sepal.Width, Species)
iris %>% arrange(!!!vars) %>% select(!!!vars)
I assumed you meaned select
rather than filter
as your question stated since iris %>% arrange(Sepal.Width, Species) %>% filter(Sepal.Width, Species)
throws an error
I think what you looking for is:
library(tidyverse)
vars <- quos(Sepal.Width, Species)
iris %>% arrange(!!!vars) %>% select(!!!vars)
I assumed you meaned select
rather than filter
as your question stated since iris %>% arrange(Sepal.Width, Species) %>% filter(Sepal.Width, Species)
throws an error
answered Nov 24 '18 at 11:18
davsjobdavsjob
70236
70236
You are right, select. I'll edit my post.
– xihh
Nov 24 '18 at 18:42
add a comment |
You are right, select. I'll edit my post.
– xihh
Nov 24 '18 at 18:42
You are right, select. I'll edit my post.
– xihh
Nov 24 '18 at 18:42
You are right, select. I'll edit my post.
– xihh
Nov 24 '18 at 18:42
add a comment |
Yes, it's a duplicate of Pass a vector of variable names to arrange() in dplyr...
library(tidyverse)
data(iris)
varList <- c("Sepal.Width","Species")
iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
...and the output:
> iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
Sepal.Width Species
1 2.0 versicolor
2 2.2 versicolor
3 2.2 versicolor
4 2.2 virginica
5 2.3 setosa
6 2.3 versicolor
7 2.3 versicolor
8 2.3 versicolor
9 2.4 versicolor
10 2.4 versicolor
11 2.4 versicolor
12 2.5 versicolor
13 2.5 versicolor
14 2.5 versicolor
15 2.5 versicolor
16 2.5 virginica
17 2.5 virginica
18 2.5 virginica
19 2.5 virginica
20 2.6 versicolor
21 2.6 versicolor
22 2.6 versicolor
23 2.6 virginica
24 2.6 virginica
...
add a comment |
Yes, it's a duplicate of Pass a vector of variable names to arrange() in dplyr...
library(tidyverse)
data(iris)
varList <- c("Sepal.Width","Species")
iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
...and the output:
> iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
Sepal.Width Species
1 2.0 versicolor
2 2.2 versicolor
3 2.2 versicolor
4 2.2 virginica
5 2.3 setosa
6 2.3 versicolor
7 2.3 versicolor
8 2.3 versicolor
9 2.4 versicolor
10 2.4 versicolor
11 2.4 versicolor
12 2.5 versicolor
13 2.5 versicolor
14 2.5 versicolor
15 2.5 versicolor
16 2.5 virginica
17 2.5 virginica
18 2.5 virginica
19 2.5 virginica
20 2.6 versicolor
21 2.6 versicolor
22 2.6 versicolor
23 2.6 virginica
24 2.6 virginica
...
add a comment |
Yes, it's a duplicate of Pass a vector of variable names to arrange() in dplyr...
library(tidyverse)
data(iris)
varList <- c("Sepal.Width","Species")
iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
...and the output:
> iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
Sepal.Width Species
1 2.0 versicolor
2 2.2 versicolor
3 2.2 versicolor
4 2.2 virginica
5 2.3 setosa
6 2.3 versicolor
7 2.3 versicolor
8 2.3 versicolor
9 2.4 versicolor
10 2.4 versicolor
11 2.4 versicolor
12 2.5 versicolor
13 2.5 versicolor
14 2.5 versicolor
15 2.5 versicolor
16 2.5 virginica
17 2.5 virginica
18 2.5 virginica
19 2.5 virginica
20 2.6 versicolor
21 2.6 versicolor
22 2.6 versicolor
23 2.6 virginica
24 2.6 virginica
...
Yes, it's a duplicate of Pass a vector of variable names to arrange() in dplyr...
library(tidyverse)
data(iris)
varList <- c("Sepal.Width","Species")
iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
...and the output:
> iris %>% arrange_(.dots=varList) %>% select_(.dots=varList)
Sepal.Width Species
1 2.0 versicolor
2 2.2 versicolor
3 2.2 versicolor
4 2.2 virginica
5 2.3 setosa
6 2.3 versicolor
7 2.3 versicolor
8 2.3 versicolor
9 2.4 versicolor
10 2.4 versicolor
11 2.4 versicolor
12 2.5 versicolor
13 2.5 versicolor
14 2.5 versicolor
15 2.5 versicolor
16 2.5 virginica
17 2.5 virginica
18 2.5 virginica
19 2.5 virginica
20 2.6 versicolor
21 2.6 versicolor
22 2.6 versicolor
23 2.6 virginica
24 2.6 virginica
...
answered Nov 24 '18 at 5:26
community wiki
Len Greski
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%2f53454013%2fdry-when-enumerating-variables-in-r-for-multiple-operations-in-a-pipeline%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
There is a big difference between DRY and what you're talking about.
– hrbrmstr
Nov 24 '18 at 2:30
2
Possible duplicate of Pass a vector of variable names to arrange() in dplyr
– Len Greski
Nov 24 '18 at 3:30
Yes, that solved my problem. I think that question should reworded more generally so that It can be found quickly.
– xihh
Nov 24 '18 at 18:46