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.
r if-statement
add a comment |
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.
r if-statement
Can you provide a sample of the data? You have several issues, the first one being the unmatched bracket herefilter(dep_rel.y %in% c("ROOT"),
there is a missing)
The syntax of an if statement isif (test_expression) { statement }
– Nakx
Nov 11 at 21:38
add a comment |
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.
r if-statement
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
r if-statement
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 herefilter(dep_rel.y %in% c("ROOT"),
there is a missing)
The syntax of an if statement isif (test_expression) { statement }
– Nakx
Nov 11 at 21:38
add a comment |
Can you provide a sample of the data? You have several issues, the first one being the unmatched bracket herefilter(dep_rel.y %in% c("ROOT"),
there is a missing)
The syntax of an if statement isif (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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
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.
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.
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%2f53252439%2fr-dplyr-filter-with-if-else-statement-not-working%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
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 isif (test_expression) { statement }
– Nakx
Nov 11 at 21:38