I recently did something kind of similar. First add a column combining year and charity. Then convert the data frame into a list of charities per individual. I called your example of the data x
x$info <- paste(x$Year,x$Charity,sep="_")
All_Groups.list <- vector(length(unique(x$Individual)),mode="list")
names(All_Groups.list) <- as.character(unique(x$Individual))
for (i in 1:length(All_Groups.list)) {
All_Groups.list[i] <- list(c(as.character(x[x$Individual == names(All_Groups.list)[i],4])))
}
Self.Cor.table <- sapply(All_Groups.list, function(x) {
sapply(All_Groups.list,function(y){
length(x[x %in% y])
})
})
The output is a correlation table where the numbers count the overlap in attended events
> Self.Cor.table
1 2 17
1 1 1 1
2 1 3 2
17 1 2 2
This differs from your desired output by giving the number of events attended by each individual instead of a .
which I think is important because each individual attends a different number of events.
If you want it per year subset the data frame by year and repeat for each subset.