Perform Task on subset rather than filter out
I am performing multiple filter steps (using Variables 1-8 in my example) using data.table. I would like to change my code in a way that rather than extracting a subset of my data table I change a boolean variable (Variable 9) within the data table from TRUE to FALSE when the row would not have been extracted before.
Until now I was trying to this somehow using the data.table package but I would also be happy with a solution using another package or baseR.
One examplary filterstep which i used to extract a subset:
DT_new <- DT[DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)][Variable6-Variable7 >= 0 | Variable8 < 100]
I would like to set it up in a way that DTnew still contains all the rows (even those rejected by my filter). However, for those rows that do not meet the criteria of my filter I would like to switch the value of a Variable9 within my data table from TRUE to FALSE.
r data.table
add a comment |
I am performing multiple filter steps (using Variables 1-8 in my example) using data.table. I would like to change my code in a way that rather than extracting a subset of my data table I change a boolean variable (Variable 9) within the data table from TRUE to FALSE when the row would not have been extracted before.
Until now I was trying to this somehow using the data.table package but I would also be happy with a solution using another package or baseR.
One examplary filterstep which i used to extract a subset:
DT_new <- DT[DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)][Variable6-Variable7 >= 0 | Variable8 < 100]
I would like to set it up in a way that DTnew still contains all the rows (even those rejected by my filter). However, for those rows that do not meet the criteria of my filter I would like to switch the value of a Variable9 within my data table from TRUE to FALSE.
r data.table
add a comment |
I am performing multiple filter steps (using Variables 1-8 in my example) using data.table. I would like to change my code in a way that rather than extracting a subset of my data table I change a boolean variable (Variable 9) within the data table from TRUE to FALSE when the row would not have been extracted before.
Until now I was trying to this somehow using the data.table package but I would also be happy with a solution using another package or baseR.
One examplary filterstep which i used to extract a subset:
DT_new <- DT[DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)][Variable6-Variable7 >= 0 | Variable8 < 100]
I would like to set it up in a way that DTnew still contains all the rows (even those rejected by my filter). However, for those rows that do not meet the criteria of my filter I would like to switch the value of a Variable9 within my data table from TRUE to FALSE.
r data.table
I am performing multiple filter steps (using Variables 1-8 in my example) using data.table. I would like to change my code in a way that rather than extracting a subset of my data table I change a boolean variable (Variable 9) within the data table from TRUE to FALSE when the row would not have been extracted before.
Until now I was trying to this somehow using the data.table package but I would also be happy with a solution using another package or baseR.
One examplary filterstep which i used to extract a subset:
DT_new <- DT[DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)][Variable6-Variable7 >= 0 | Variable8 < 100]
I would like to set it up in a way that DTnew still contains all the rows (even those rejected by my filter). However, for those rows that do not meet the criteria of my filter I would like to switch the value of a Variable9 within my data table from TRUE to FALSE.
r data.table
r data.table
asked Jan 2 at 13:09
yaselyasel
727
727
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Maybe if you try like this:
DT_tmp <- DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)]
DT$Variable9 <- ifelse( (DT_tmp$Variable6-DT_tmp$Variable7 >= 0 | DT_tmp$Variable8 < 100), T, F)
is this what you where looking for?
Hmm isn't theon=.(Variable3,Variable4, Variable5)
part missing then?
– yasel
Jan 2 at 13:34
What exactly are you looking forward with the on=.() part?
– karen
Jan 2 at 13:35
I asked about how to set up I filter like that some time ago. The reason why it looks like that is explained here [stackoverflow.com/questions/53399216/…
– yasel
Jan 2 at 13:40
I have edited the answer, does this make sense now? I am very impressed with the on functionality by the way ;)
– karen
Jan 2 at 13:59
1
What is theifelse
for? A logical test already returnsTREU
orFALSE
, e.g, its like doingifelse(2 > 1, TRUE, FLASE)
instead of just2 > 1
– David Arenburg
Jan 3 at 8:56
|
show 6 more comments
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%2f54006969%2fperform-task-on-subset-rather-than-filter-out%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
Maybe if you try like this:
DT_tmp <- DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)]
DT$Variable9 <- ifelse( (DT_tmp$Variable6-DT_tmp$Variable7 >= 0 | DT_tmp$Variable8 < 100), T, F)
is this what you where looking for?
Hmm isn't theon=.(Variable3,Variable4, Variable5)
part missing then?
– yasel
Jan 2 at 13:34
What exactly are you looking forward with the on=.() part?
– karen
Jan 2 at 13:35
I asked about how to set up I filter like that some time ago. The reason why it looks like that is explained here [stackoverflow.com/questions/53399216/…
– yasel
Jan 2 at 13:40
I have edited the answer, does this make sense now? I am very impressed with the on functionality by the way ;)
– karen
Jan 2 at 13:59
1
What is theifelse
for? A logical test already returnsTREU
orFALSE
, e.g, its like doingifelse(2 > 1, TRUE, FLASE)
instead of just2 > 1
– David Arenburg
Jan 3 at 8:56
|
show 6 more comments
Maybe if you try like this:
DT_tmp <- DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)]
DT$Variable9 <- ifelse( (DT_tmp$Variable6-DT_tmp$Variable7 >= 0 | DT_tmp$Variable8 < 100), T, F)
is this what you where looking for?
Hmm isn't theon=.(Variable3,Variable4, Variable5)
part missing then?
– yasel
Jan 2 at 13:34
What exactly are you looking forward with the on=.() part?
– karen
Jan 2 at 13:35
I asked about how to set up I filter like that some time ago. The reason why it looks like that is explained here [stackoverflow.com/questions/53399216/…
– yasel
Jan 2 at 13:40
I have edited the answer, does this make sense now? I am very impressed with the on functionality by the way ;)
– karen
Jan 2 at 13:59
1
What is theifelse
for? A logical test already returnsTREU
orFALSE
, e.g, its like doingifelse(2 > 1, TRUE, FLASE)
instead of just2 > 1
– David Arenburg
Jan 3 at 8:56
|
show 6 more comments
Maybe if you try like this:
DT_tmp <- DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)]
DT$Variable9 <- ifelse( (DT_tmp$Variable6-DT_tmp$Variable7 >= 0 | DT_tmp$Variable8 < 100), T, F)
is this what you where looking for?
Maybe if you try like this:
DT_tmp <- DT[Variable1=="M+H" & Variable2==100], on=.(Variable3,
Variable4, Variable5)]
DT$Variable9 <- ifelse( (DT_tmp$Variable6-DT_tmp$Variable7 >= 0 | DT_tmp$Variable8 < 100), T, F)
is this what you where looking for?
edited Jan 2 at 13:58
answered Jan 2 at 13:17
karenkaren
487215
487215
Hmm isn't theon=.(Variable3,Variable4, Variable5)
part missing then?
– yasel
Jan 2 at 13:34
What exactly are you looking forward with the on=.() part?
– karen
Jan 2 at 13:35
I asked about how to set up I filter like that some time ago. The reason why it looks like that is explained here [stackoverflow.com/questions/53399216/…
– yasel
Jan 2 at 13:40
I have edited the answer, does this make sense now? I am very impressed with the on functionality by the way ;)
– karen
Jan 2 at 13:59
1
What is theifelse
for? A logical test already returnsTREU
orFALSE
, e.g, its like doingifelse(2 > 1, TRUE, FLASE)
instead of just2 > 1
– David Arenburg
Jan 3 at 8:56
|
show 6 more comments
Hmm isn't theon=.(Variable3,Variable4, Variable5)
part missing then?
– yasel
Jan 2 at 13:34
What exactly are you looking forward with the on=.() part?
– karen
Jan 2 at 13:35
I asked about how to set up I filter like that some time ago. The reason why it looks like that is explained here [stackoverflow.com/questions/53399216/…
– yasel
Jan 2 at 13:40
I have edited the answer, does this make sense now? I am very impressed with the on functionality by the way ;)
– karen
Jan 2 at 13:59
1
What is theifelse
for? A logical test already returnsTREU
orFALSE
, e.g, its like doingifelse(2 > 1, TRUE, FLASE)
instead of just2 > 1
– David Arenburg
Jan 3 at 8:56
Hmm isn't the
on=.(Variable3,Variable4, Variable5)
part missing then?– yasel
Jan 2 at 13:34
Hmm isn't the
on=.(Variable3,Variable4, Variable5)
part missing then?– yasel
Jan 2 at 13:34
What exactly are you looking forward with the on=.() part?
– karen
Jan 2 at 13:35
What exactly are you looking forward with the on=.() part?
– karen
Jan 2 at 13:35
I asked about how to set up I filter like that some time ago. The reason why it looks like that is explained here [stackoverflow.com/questions/53399216/…
– yasel
Jan 2 at 13:40
I asked about how to set up I filter like that some time ago. The reason why it looks like that is explained here [stackoverflow.com/questions/53399216/…
– yasel
Jan 2 at 13:40
I have edited the answer, does this make sense now? I am very impressed with the on functionality by the way ;)
– karen
Jan 2 at 13:59
I have edited the answer, does this make sense now? I am very impressed with the on functionality by the way ;)
– karen
Jan 2 at 13:59
1
1
What is the
ifelse
for? A logical test already returns TREU
or FALSE
, e.g, its like doing ifelse(2 > 1, TRUE, FLASE)
instead of just 2 > 1
– David Arenburg
Jan 3 at 8:56
What is the
ifelse
for? A logical test already returns TREU
or FALSE
, e.g, its like doing ifelse(2 > 1, TRUE, FLASE)
instead of just 2 > 1
– David Arenburg
Jan 3 at 8:56
|
show 6 more comments
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%2f54006969%2fperform-task-on-subset-rather-than-filter-out%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