R Delete data frame lines by condition

Asked

Viewed 309 times

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 the "3" character appears, but only those that are present in all "I" columns, 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

1 answer

6


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

x[apply(x[-1] != 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.