I am trying to use dpylr filter function in R to remove rows based on certain conditions. Here is sample data.
df = data.frame(chr = c(1,1,1,2,2,3,3),
pos = c(10,15,20,10,14,2,15)
I need to exclude a range of values in a specific region so I thought something like this would work.
library(dpylr)
filter(df, chr == "1" & !pos %in% (1:15)
However my output only give me:
chr pos
1 20
I lose all other rows like data from chr 2 and 3. My desired output looks like this:
chr pos
1 20
2 10
2 14
3 2
3 15
How can I get an output where my exclusion criteria only effects a selected value like (i.e. chr == 1), but still retains data from other chr and chr 1 that are not included in my range.
CodePudding user response:
Your code is almost correct, you need to apply the !
operator to the whole condition:
Code
df %>%
filter(!(chr == 1 & pos %in% (1:15)))
Output
chr pos
1 1 20
2 2 10
3 2 14
4 3 2
5 3 15