I have a not complicated problem, I think, but my knowledge of R is pretty basic and so I can't find an answer. I have 4 variables. One is a grouping variable I call cluster. The other 3 (ID, IDman, IDwoman) are IDs of individuals. Something like this:
cluster <- c("a", "a", "a", "b", "b", "b", "c", "c", "c")
ID <- c(1, 7, 18, 3, 3, 9, 25, 10, 19)
IDman <- c(1, 2, 3, 3, 3, 4, 10, 10, 6)
IDwoman <- c(5, 7, 9, 11, 12, 14, 19,19,5)
households <- data.frame(cluster, ID, IDman, IDwoman)
The dataframe (household) is basically showing the individuals (ID) that are in a household (cluster). Sometimes, those individuals are a marriage, and this information is given by a certain combination of IDman and IDwoman: it happens when ID equals IDman and ID equals IDwoman within the same cluster. For example, for the first cluster (cluster=a, or first 3 rows) there is a marriage. IDman=1 and IDwoman=7 are a marriage because they are in the same household (cluster=a) and because ID and IDman equal 1 in the first row, but also ID and IDwoman equal 7 in the second (all of it happening within cluster a). 
So, what I need is to find the number of unique combinations for each cluster of ID-equals-IDman and ID-equals-IDwoman. For instance,in the second cluster, we have none (as there is no IDwoman=9), and in the third cluster we have again one, as IDman=10 and IDwoman=19 appear both in ID, and the repetition of the observation IDman=10 and IDwoman=19 is not taken into account. The outcome doesn't need to be dataset showing these links. Just the number of these unique combinations per cluster. 
I don't know how to solve this. I was trying things through apply or sapply functions, but none worked.
Any idea is very welcome.
Thank you!
 
     
    