I have a huge dataframe, but here is a very simplified example:
df <- data.frame(Id=c(rep("Mike",8)), Year=c(rep("2015",2),rep("2016",3),
rep("2017",3)),location=c(rep("A",2),rep("B",3),"D","E","E"))
df
# Id Year location
#1 Mike 2015 A
#2 Mike 2015 A
#3 Mike 2016 B
#4 Mike 2016 B
#5 Mike 2016 B
#6 Mike 2017 D
#7 Mike 2017 E
#8 Mike 2017 E
My grouping criteria is Id
and Year
, so for an specific group (e.g., Mike 2017) there are many rows. I want to remove all rows of a group in which "location" factors are not all equal.
In this case the only group in which not all locations are the same is "Mike 2017". Then, I want to end up with a dataframe like this:
# Id Year location
#1 Mike 2015 A
#2 Mike 2015 A
#3 Mike 2016 B
#4 Mike 2016 B
#5 Mike 2016 B
Is there a way to do this by indicating the grouping criteria and the exclusion criteria described above?