Reputation: 77
I have a df where I want to filter for values in one column, based on if there is a specific value in the other column. For example I have a df like this:
x <- data.frame("Annotation" = c("lncRNA", "no lncRNA", "lncRNA", "no lncRNA", "lncRNA"),
"padj" = c(0.2, NA, 0.03, NA, 0.08))
A couple of things I've tried are
x <- x[ x$padj <= 0.1, ]
and
x <- x %>%
filter(case_when(Annotation != "no lncRNA" ~ padj <= 0.1))
but neither give me the output I need. They produce these respectively
Annotation padj
NA <NA> NA
3 lncRNA 0.03
NA.1 <NA> NA
5 lncRNA 0.08
Annotation padj
1 lncRNA 0.03
2 lncRNA 0.08
instead of this which is what I'd like
Annotation padj
1 no lncRNA NA
2 lncRNA 0.03
3 no lncRNA NA
4 lncRNA 0.08
Seems like there would be a simple solution, can anyone help? Thanks!
Upvotes: 0
Views: 271
Reputation: 106
Could do something like this. May not be the most elegant solution.
sel <- x$padj <= 0.1
sel[is.na(sel)] <- TRUE
filtered <- x[sel, ]
row.names(filtered) <- seq_len(nrow(filtered)) # Only if you want to reset row names
Upvotes: 0
Reputation: 541
This will do it for you.
filter(x, padj <= 0.1 | is.na(padj))
Data:
x <- data.frame("Annotation" = c("lncRNA", "no lncRNA", "lncRNA", "no lncRNA", "lncRNA"),
"padj" = c(0.2, NA, 0.03, NA, 0.08), stringsAsFactors = F)
#Result:
#Annotation padj
#1 no lncRNA NA
#2 lncRNA 0.03
#3 no lncRNA NA
#4 lncRNA 0.08
Upvotes: 1