Q:

Identify unique observations that satisfy two conditions and then remove R

I have a df as follows:
data
   names  fruit
7   john  apple
13  john orange
14  john  apple
2   mary orange
5   mary  apple
8   mary orange
10  mary  apple
12  mary  apple
1    tom  apple
6    tom  apple

I'd like to do two things. First, count the number of unique observations that have both an apple and an orange (i.e 2 mary and john). 
Then after that, I'd like to remove them from my data frame, so that I am left only with unique individuals who got apples only. 
This is what I've tried
toremove<-unique(data[data$fruit=='apple' & data$fruit=='orange',"names"])  ##this part doesn't work, if it had I would have used the below code to remove the names identified
data2<-data[!data$names %in% toremove,]

Really, I wanted to use grepl because my real data are a little more complicated than fruit. This is what I've tried (converted to a data.table first)
data1<-data.table(data1)
z<-data1[,ind := grepl('app.*? & orang.*?', fruit), by='names']  ## this works fine when i just use 'app.*?' but collapses when I try to add the & sign, so I'm making an error with the operator. In addition the by='names' doesn't work out for me, which is important. My plan here was to create an indicator (if an individual has an apple and an orange, then they get an indicator==1 and I would then filter them out on the basis of this indicator). 

So, in summary my problem is in identifying people who have both an apple and an orange. This seems so straightforward, so feel free to direct me to a resource that can teach me this!
desired output
names fruit
1   tom apple
6   tom apple

A:

If you only looking for names with apples only, here's a simple data.table approach
setDT(data)[ , if(all(fruit == "apple")) .SD, by = names]
#    names fruit
# 1:   tom apple
# 2:   tom apple

For unique observations that have both "apple" and " orange" count, you could do something like
data[, any(fruit == "apple") & any(fruit == "orange"), by = names][, sum(V1)]
## [1] 2 

Finally, if all you looking for is the users with only one unique fruit, you could try to condition using uniqueN from the devel version on GH (or length(unique()))
data[, if(uniqueN(fruit) < 2L) .SD, by = names]
#    names fruit
# 1:   tom apple
# 2:   tom apple

