R dplyr filter with if else statement not working











up vote
-1
down vote

favorite












I am trying to get this to work:



test.ROOT.triples <- triples %>%
filter(dep_rel.y %in% c("ROOT"),
{
if(verb_head_token_id == neg.adverb_head_token_id) filter (subject_head_token_id == neg.adverb_head_token_id,
verb_head_token_id == neg.adverb_head_token_id,
verb_token_id == object_head_token_id)
else
filter(subject_head_token_id == verb_head_token_id,
verb_token_id == object_head_token_id))
}


My syntax isn't quite right. I get a message about unmatched opening and closing brackets.



This code works if it's not being used in an if/else statement, but I would like to use a dplyr filter in an if/else statement.



Thank you in advance.










share|improve this question






















  • Can you provide a sample of the data? You have several issues, the first one being the unmatched bracket here filter(dep_rel.y %in% c("ROOT"), there is a missing ) The syntax of an if statement is if (test_expression) { statement }
    – Nakx
    Nov 11 at 21:38















up vote
-1
down vote

favorite












I am trying to get this to work:



test.ROOT.triples <- triples %>%
filter(dep_rel.y %in% c("ROOT"),
{
if(verb_head_token_id == neg.adverb_head_token_id) filter (subject_head_token_id == neg.adverb_head_token_id,
verb_head_token_id == neg.adverb_head_token_id,
verb_token_id == object_head_token_id)
else
filter(subject_head_token_id == verb_head_token_id,
verb_token_id == object_head_token_id))
}


My syntax isn't quite right. I get a message about unmatched opening and closing brackets.



This code works if it's not being used in an if/else statement, but I would like to use a dplyr filter in an if/else statement.



Thank you in advance.










share|improve this question






















  • Can you provide a sample of the data? You have several issues, the first one being the unmatched bracket here filter(dep_rel.y %in% c("ROOT"), there is a missing ) The syntax of an if statement is if (test_expression) { statement }
    – Nakx
    Nov 11 at 21:38













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am trying to get this to work:



test.ROOT.triples <- triples %>%
filter(dep_rel.y %in% c("ROOT"),
{
if(verb_head_token_id == neg.adverb_head_token_id) filter (subject_head_token_id == neg.adverb_head_token_id,
verb_head_token_id == neg.adverb_head_token_id,
verb_token_id == object_head_token_id)
else
filter(subject_head_token_id == verb_head_token_id,
verb_token_id == object_head_token_id))
}


My syntax isn't quite right. I get a message about unmatched opening and closing brackets.



This code works if it's not being used in an if/else statement, but I would like to use a dplyr filter in an if/else statement.



Thank you in advance.










share|improve this question













I am trying to get this to work:



test.ROOT.triples <- triples %>%
filter(dep_rel.y %in% c("ROOT"),
{
if(verb_head_token_id == neg.adverb_head_token_id) filter (subject_head_token_id == neg.adverb_head_token_id,
verb_head_token_id == neg.adverb_head_token_id,
verb_token_id == object_head_token_id)
else
filter(subject_head_token_id == verb_head_token_id,
verb_token_id == object_head_token_id))
}


My syntax isn't quite right. I get a message about unmatched opening and closing brackets.



This code works if it's not being used in an if/else statement, but I would like to use a dplyr filter in an if/else statement.



Thank you in advance.







r if-statement






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 19:35









generic

112




112












  • Can you provide a sample of the data? You have several issues, the first one being the unmatched bracket here filter(dep_rel.y %in% c("ROOT"), there is a missing ) The syntax of an if statement is if (test_expression) { statement }
    – Nakx
    Nov 11 at 21:38


















  • Can you provide a sample of the data? You have several issues, the first one being the unmatched bracket here filter(dep_rel.y %in% c("ROOT"), there is a missing ) The syntax of an if statement is if (test_expression) { statement }
    – Nakx
    Nov 11 at 21:38
















Can you provide a sample of the data? You have several issues, the first one being the unmatched bracket here filter(dep_rel.y %in% c("ROOT"), there is a missing ) The syntax of an if statement is if (test_expression) { statement }
– Nakx
Nov 11 at 21:38




Can you provide a sample of the data? You have several issues, the first one being the unmatched bracket here filter(dep_rel.y %in% c("ROOT"), there is a missing ) The syntax of an if statement is if (test_expression) { statement }
– Nakx
Nov 11 at 21:38












1 Answer
1






active

oldest

votes

















up vote
0
down vote













If else statements within dplyr are of a particular sort of syntax, I never use them within Filter and I'm not sure if that even works. One solution may be to just filter down two separate data frames and join them later if you want.



I am separating the filter statements for ease of reading, but you can have them all in the same filter statement separated by commas.



triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(subject_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> FirstDF

triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(subject_head_token_id == verb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> SecondDF


You can look up rbind to re-attach them (if you even want that, I imagine downstream analyses would be different so having them separate may be cleaner).



Just to show you of how the if else statements work within dplyr pipeline, you could create an index to mark the rows of different qualities with a new variable. The structure of IF Else in dplyr is demonstrated:



NewItem = if_else(logicals, WhatHappensIfTrue, WhatHappensIfFalse)

triples %>% mutate(Category = if_else(verb_head_token_id == neg.adverb_head_token_id &
subject_head_token_id == neg.adverb_head_token_id &
verb_head_token_id == neg.adverb_head_token_id &
verb_token_id == object_head_token_id, #comma denotes end of "if" logical
1, #This 1 is what happens if logical(s) TRUE
0)) #The zero is "else" - when logical(s) FALSE. It CAN be another if_else.





share|improve this answer























  • Okay, so I am pretty new to if/else statement. I'm deff coding in R - my code works fine outside of the if/else statement and I am trying to learn something new. Thanks for the comments.
    – generic
    Nov 11 at 20:00











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',
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%2f53252439%2fr-dplyr-filter-with-if-else-statement-not-working%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








up vote
0
down vote













If else statements within dplyr are of a particular sort of syntax, I never use them within Filter and I'm not sure if that even works. One solution may be to just filter down two separate data frames and join them later if you want.



I am separating the filter statements for ease of reading, but you can have them all in the same filter statement separated by commas.



triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(subject_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> FirstDF

triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(subject_head_token_id == verb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> SecondDF


You can look up rbind to re-attach them (if you even want that, I imagine downstream analyses would be different so having them separate may be cleaner).



Just to show you of how the if else statements work within dplyr pipeline, you could create an index to mark the rows of different qualities with a new variable. The structure of IF Else in dplyr is demonstrated:



NewItem = if_else(logicals, WhatHappensIfTrue, WhatHappensIfFalse)

triples %>% mutate(Category = if_else(verb_head_token_id == neg.adverb_head_token_id &
subject_head_token_id == neg.adverb_head_token_id &
verb_head_token_id == neg.adverb_head_token_id &
verb_token_id == object_head_token_id, #comma denotes end of "if" logical
1, #This 1 is what happens if logical(s) TRUE
0)) #The zero is "else" - when logical(s) FALSE. It CAN be another if_else.





share|improve this answer























  • Okay, so I am pretty new to if/else statement. I'm deff coding in R - my code works fine outside of the if/else statement and I am trying to learn something new. Thanks for the comments.
    – generic
    Nov 11 at 20:00















up vote
0
down vote













If else statements within dplyr are of a particular sort of syntax, I never use them within Filter and I'm not sure if that even works. One solution may be to just filter down two separate data frames and join them later if you want.



I am separating the filter statements for ease of reading, but you can have them all in the same filter statement separated by commas.



triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(subject_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> FirstDF

triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(subject_head_token_id == verb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> SecondDF


You can look up rbind to re-attach them (if you even want that, I imagine downstream analyses would be different so having them separate may be cleaner).



Just to show you of how the if else statements work within dplyr pipeline, you could create an index to mark the rows of different qualities with a new variable. The structure of IF Else in dplyr is demonstrated:



NewItem = if_else(logicals, WhatHappensIfTrue, WhatHappensIfFalse)

triples %>% mutate(Category = if_else(verb_head_token_id == neg.adverb_head_token_id &
subject_head_token_id == neg.adverb_head_token_id &
verb_head_token_id == neg.adverb_head_token_id &
verb_token_id == object_head_token_id, #comma denotes end of "if" logical
1, #This 1 is what happens if logical(s) TRUE
0)) #The zero is "else" - when logical(s) FALSE. It CAN be another if_else.





share|improve this answer























  • Okay, so I am pretty new to if/else statement. I'm deff coding in R - my code works fine outside of the if/else statement and I am trying to learn something new. Thanks for the comments.
    – generic
    Nov 11 at 20:00













up vote
0
down vote










up vote
0
down vote









If else statements within dplyr are of a particular sort of syntax, I never use them within Filter and I'm not sure if that even works. One solution may be to just filter down two separate data frames and join them later if you want.



I am separating the filter statements for ease of reading, but you can have them all in the same filter statement separated by commas.



triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(subject_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> FirstDF

triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(subject_head_token_id == verb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> SecondDF


You can look up rbind to re-attach them (if you even want that, I imagine downstream analyses would be different so having them separate may be cleaner).



Just to show you of how the if else statements work within dplyr pipeline, you could create an index to mark the rows of different qualities with a new variable. The structure of IF Else in dplyr is demonstrated:



NewItem = if_else(logicals, WhatHappensIfTrue, WhatHappensIfFalse)

triples %>% mutate(Category = if_else(verb_head_token_id == neg.adverb_head_token_id &
subject_head_token_id == neg.adverb_head_token_id &
verb_head_token_id == neg.adverb_head_token_id &
verb_token_id == object_head_token_id, #comma denotes end of "if" logical
1, #This 1 is what happens if logical(s) TRUE
0)) #The zero is "else" - when logical(s) FALSE. It CAN be another if_else.





share|improve this answer














If else statements within dplyr are of a particular sort of syntax, I never use them within Filter and I'm not sure if that even works. One solution may be to just filter down two separate data frames and join them later if you want.



I am separating the filter statements for ease of reading, but you can have them all in the same filter statement separated by commas.



triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(subject_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_head_token_id == neg.adverb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> FirstDF

triples %>%
filter(dep_rel.y %in% c("ROOT")) %>%
filter(subject_head_token_id == verb_head_token_id) %>%
filter(verb_token_id == object_head_token_id) -> SecondDF


You can look up rbind to re-attach them (if you even want that, I imagine downstream analyses would be different so having them separate may be cleaner).



Just to show you of how the if else statements work within dplyr pipeline, you could create an index to mark the rows of different qualities with a new variable. The structure of IF Else in dplyr is demonstrated:



NewItem = if_else(logicals, WhatHappensIfTrue, WhatHappensIfFalse)

triples %>% mutate(Category = if_else(verb_head_token_id == neg.adverb_head_token_id &
subject_head_token_id == neg.adverb_head_token_id &
verb_head_token_id == neg.adverb_head_token_id &
verb_token_id == object_head_token_id, #comma denotes end of "if" logical
1, #This 1 is what happens if logical(s) TRUE
0)) #The zero is "else" - when logical(s) FALSE. It CAN be another if_else.






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 18:32

























answered Nov 11 at 19:53









Michael

1437




1437












  • Okay, so I am pretty new to if/else statement. I'm deff coding in R - my code works fine outside of the if/else statement and I am trying to learn something new. Thanks for the comments.
    – generic
    Nov 11 at 20:00


















  • Okay, so I am pretty new to if/else statement. I'm deff coding in R - my code works fine outside of the if/else statement and I am trying to learn something new. Thanks for the comments.
    – generic
    Nov 11 at 20:00
















Okay, so I am pretty new to if/else statement. I'm deff coding in R - my code works fine outside of the if/else statement and I am trying to learn something new. Thanks for the comments.
– generic
Nov 11 at 20:00




Okay, so I am pretty new to if/else statement. I'm deff coding in R - my code works fine outside of the if/else statement and I am trying to learn something new. Thanks for the comments.
– generic
Nov 11 at 20:00


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53252439%2fr-dplyr-filter-with-if-else-statement-not-working%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

Bressuire

Vorschmack

Quarantine