Uing R, I want to count the number of occurences in two variables by two other variables; IDS and year. One of the variables counted need to be counted by unique value. I have really looked around for an answer to this but I cannot seem to find it. I have a dataset like this (though including many more variables):
IDS = c(1,1,1,1,1,1,2,2) 
year = c(1,1,1,1,1,2,1,1) 
x = c(5, 5, 5, 10, 2, NA, 3, 3)
y = c(1, 2, 4, 0, NA, 2, 0, NA)
dfxy = data.frame(IDS, year, x, y)
dfxy
   IDS year   x  y
1   1    1    5  1
2   1    1    5  2
3   1    1    5  4
4   1    1   10  0
5   1    1    2 NA
6   1    2   NA  2
7   2    1    3  0
8   2    1    3 NA
I want a count of the number of occurences in the two columns x and y by each IDS and each year. The count in x needs to be by unique value of x. I want an output like this:
   IDS year x y
1   1    1  3 4
2   1    2  0 1
3   2    1  1 1
It is similar to the answer with cbind in
Aggregate / summarize multiple variables per group (i.e. sum, mean, etc)
which for me would look like
aggregate(cbind(x, y)~IDS+year, data=dfxy, ???)
NA counts as no occurence, any number counts as an occurence in y, in x each unique occurence must be counted (as long as it is not NA). There are no rows with NA in both x and y. I have tried using length instead of sum, but this only seem to summarize the number of rows equally for both x and y.
Ideas or a link I can find an answer to this in? Thanks
 
     
     
    