0

I am having trouble filtering my dataframe which is the following:

df <- data.frame(A = c("q","q","z","z","v","v"),
                  B = c("a","b","a","b","b","a"),
                  C = c( 10, 12, 5, 0, 0, 0))

Now I do want to get rid of the 0s since they mean NA, but only if both values for a value in A are so. So I do want to keep both Zs but want to get rid of both Vs. This is my desired output:

A B C
q a 10
q b 12
z a 5
z b 0

However i can only get it to show this(with all 0 values included):

A B C
q a 10
q b 12
z a 5
z b 0
v b 0
v a 0

Or with Bs 0 value removed aswell. I already tried removing values below 0 but this removed the z value aswell, which I need for my analysis.

How can I get the desired output?

www
  • 38,575
  • 12
  • 48
  • 84
  • 2
    `df[!with(df, ave(C, A, FUN = function(i) all(i==0))),]`, but there is probably a dupe. I am searching for one now – Sotos Jul 13 '17 at 14:34
  • 2
    A solution using dplyr: `library(dplyr) df2 <- df %>% group_by(A) %>% filter(!all(C == 0))` – www Jul 13 '17 at 14:40

0 Answers0