R Delete rows from the date frame by condition

3

I have the following data set:

Nome <- c("Laetia","Vouacapoua","Lecythis","Pourouma","Poraqueiba","Pseudopiptadenia", "Abarema");
I1 <- c(1,3,3,2,3,3,3);
I2 <- c(1,3,1,3,3,3,3);
I3 <- c(1,3,1,3,2,3,3);
I4 <- c(1,3,2,2,3,3,3)

x <- data.frame(Nome,I1,I2,I3,I4)

example:

              Nome I1 I2 I3 I4
1           Laetia  1  1  1  1
2       Vouacapoua  3  3  3  3
3         Lecythis  3  1  1  2
4         Pourouma  2  3  3  2
5       Poraqueiba  3  3  2  3
6 Pseudopiptadenia  3  3  3  3
7          Abarema  3  3  3  3

I would like to exclude the rows in which rows appear the character "3", but only those that are present in all columns "I", generating this result:

              Nome I1 I2 I3 I4
1           Laetia  1  1  1  1
3         Lecythis  3  1  1  2
4         Pourouma  2  3  3  2
5       Poraqueiba  3  3  2  3
    
asked by anonymous 26.07.2018 / 04:56

1 answer

4

You can apply any() to each row to keep those that have at least one cell different from the condition:

x[apply(x[,2:ncol(x)] != 3, 1, any),]

        Nome I1 I2 I3 I4
1     Laetia  1  1  1  1
3   Lecythis  3  1  1  2
4   Pourouma  2  3  3  2
5 Poraqueiba  3  3  2  3

If you are working with numbers rather than characters, a lazier way is to use sum to cut the lines:

x[rowSums(x[2:ncol(x)]) != (ncol(x)-1)*3,]

But this type of approach is not generally recommended.

    
26.07.2018 / 08:29