Reputation: 916
I have a dataset df that looks like this
rel freq key
1.1 1.1 a
1 1 b
1 NA c
NA NA d
NA 0 e
NA 1 f
I want to obtain this:
rel freq key
1.1 1.1 a
1 1 b
1 NA c
NA 1 f
basically I want to get rid of the rows where freq
= 0 or the rows where both rel
and freq
are equal to NA.
I tried
df <- df[df$freq!=0,]
to filter out freq = 0 but it gives me some problems
similarly
library(dplyr)
df <- filter(df, freq != 0)
eliminates all the rows with freq
= NA
Any suggestion on how I can solve this?
Upvotes: 0
Views: 46
Reputation: 1618
df[!(!is.na(df$freq) & df$freq == 0 | (is.na(df$freq) & is.na(df$rel))),]
# rel freq key
# 1 1.1 1.1 a
# 2 1.0 1.0 b
# 3 1.0 NA c
# 6 NA 1.0 f
Data
df <- data.frame(rel = c(1.1, 1, 1, NA, NA, NA),
freq = c(1.1, 1, NA, NA, 0, 1),
key = letters[1:6])
Upvotes: 1
Reputation: 14764
Try:
subset(df, !(freq == 0 & !is.na(freq) | (is.na(freq) & is.na(rel))))
Output:
rel freq key
1 1.1 1.1 a
2 1.0 1.0 b
3 1.0 NA c
6 NA 1.0 f
Or in dplyr
:
dplyr::filter(df, !(freq == 0 & !is.na(freq) | (is.na(freq) & is.na(rel))))
Upvotes: 2