I have two tables. Which have the kind of formatting shown below. One of it is table A as such:
students|Test Score|Year
A | 100 |1993
B | 81 |1992
C | 92 |1992
D | 88 |1993
Another table B I have looks like this:
Class | Students | Year
1 | {A,D} |1993
2 | {B,C} |1992
I would like to perform some sort of manipulation in R where by I can search for the students listed in the array under the column in table B from table A and tabulate the scores into the following format:
Class | Students | Mean Score
1 | {A,D} | 94
2 | {B,C} | 86.5
Is there any formula which I can use to do the searching and then merge those results by some manipulation in R?
I know the above can be done with:
B$MeanScore <- sapply(strsplit(gsub("[{}]","", B$Students), split=","),
function(x) mean(A$Test.Score[A$Students %in% x]))
But is there a way for me to add a second condition which is to match the year as well. The year of the class as well as the year of the test.