Given the following data.frame:
t x y
---------
1 1 3
1 1 3
1 1 2
2 1 2
2 2 2
I would like output of the form
t cnt cux cuy
---------------
1 3 1 2
2 2 2 1
where cnt is the count of all rows with a particular value t, cux/cuy is the count of all unique rows of x/y
The other constraint is that the answer must work for a variable number of columns.
Thanks.
What you describe in words and what you show in expected output do not agree. In particular, counting unique values of
ywould be 2 and 1, not 3 and 2 based on your input. Going with the written description:Or if you want something really functional looking:
Or going completely overboard with the functional paradigm:
None of these give the column names you asked for; instead of
cuxit isx. However, they can be changed afterward.which gives