I have a data.frame in R. I want to select rows that have a specific value in one of three columns. I have a large data.frame, but what I need is like the example below:
seller<-c("Mary", "Bill", "Jeff", "Paty", "Paul", "Criss")
c1<-c(1,2,1,1,3,1)
c2<-c(1,1,1,1,2,1)
c3<-c(1,1,3,1,1,1)
data<-data.frame(seller, c1, c2, c3)
>data
seller c1 c2 c3
1 Mary 1 1 1
2 Bill 2 1 1
3 Jeff 1 1 3
4 Paty 1 1 1
5 Paul 3 2 1
6 Cris 1 1 1
I want to select the rows that have values equal and greater than 2, like this:
>data
seller c1 c2 c3
1 Bill 2 1 1
2 Jeff 1 1 3
3 Paul 3 2 1
Thanks for the help!
I don’t know if this is the best way to do it, but you can use
rowSumsandwhichto subset, as in the following example:A more direct alternative is:
I’ve used
data[-1]to remove the names of the sellers (as they are character strings). Then,applywith margin1applies the specified function by row (margin2would do the same by column). The function isany(x >= 2), which returnsTRUEorFALSEfor each row; withwhich, we can subset the rows for which the return wasTRUE.