I am working through some data and trying to do some conditional filtering. I want to write a statement that assesses if one variable is equal to a number (in this case, 1) and, if so, filters based on the value of another column. The result should be that all AtBatPitchSequences == 1 also have PitchType == "FA".
- Please note that if AtBatPitchSequence > 1 it should not be filtered, so row 4 should be kept after the filter
My data (firsttwopitches) looks like this:
YearID GameID GamePitchSequen~ PAofInning AtBatPitchSeque~ Inning Balls Strikes PitchType
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 2018 DFCBC~ 1 1 1 1 0 0 FA
2 2018 DFCBC~ 2 1 2 1 1 0 FA
3 2018 DFCBC~ 4 2 1 1 0 0 FA
4 2018 DFCBC~ 5 2 2 1 0 1 SI
5 2018 DFCBC~ 8 3 1 1 0 0 FA
6 2018 DFCBC~ 9 3 2 1 0 1 FA
To address this problem, I am trying to use an if statement:
library(tidyverse)
firsttwopitches %>%
if (AtBatPitchSequence == 1) {
filter(PitchType == "FA")
}
However, this raises an error and a warning:
Error in if (.) AtBatPitchSequence == 1 else { :
argument is not interpretable as logical
In addition: Warning message:
In if (.) AtBatPitchSequence == 1 else { :
the condition has length > 1 and only the first element will be used
I do not understand why my argument is not interpretable as logical. In my head, it should assess whether AtBatPitchSequence equals 1 or not, then move on to the next row. Also, what does the warning message mean? If this warning is dealt with by correcting my if statement, don't worry about it, but I am still new and am trying to debug my own work better. I read through this Error in if/while (condition) : argument is not interpretable as logical question and others to try and find my error but was unsuccessful.
Thank you very much