I am trying to filter my data frame to leave the highest value of one variable (Question), for each combination of two others (Person and Test).
My data.frame looks something like this:
df <- data.frame(Person=c("Person1","Person1","Person1","Person2","Person2","Person2","Person3","Person3","Person3"),
Test=c(rep("Test1",9)),
Question=c("1","2","3","1","2","3","1","2","3"))
Except that there are multiple tests i.e. Test2, Test3 etc.
I want to filter down to show the last Question in each Test for every Person. There are a different number of Questions in each Test.
Using the response to this question: dplyr filter: Get rows with minimum of variable, but only the first if multiple minima, I managed to get some of the way with:
library(dplyr)
df.grouped <- group_by(df.orginial, Person, Test)
df.lastquestion <- filter(df.grouped, Question == max(Question))
Unfortunately, it leaves me with the highest Question number that each Person answered across all Tests. Whereas, I would like the highest Question number that each Person answered on each Test.
Thanks