I want to extend the solution of this post where @AnandaMahto gave a very elegant solution to my problem.
For this new function, I'd like that if there are several times the same species in the same house, it will count only one observation. One house with two cats
and one rat
does not create two observations between cat
and rat
but only one (As shown below)
In this example, there are two rats
in the house number 4. As already said, I do not want to consider two observations between rat
and cat
and between spider
and rat
but only one observation between rat
and cat
and one observation between spider
and rat
houses = c(1,1,2,3,4,4,4,4,5,6,5)
animals = c('cat','dog','cat','dog','rat', 'cat', 'spider', 'rat', 'cat', 'cat', 'rat')
@AnandaMahto's solution would return this:
dog rat spider
cat 1 3 1
dog 0 0
rat 2
But I would like to get this:
dog rat spider
cat 1 2 1
dog 0 0
rat 1