this is related to this question that I've looked at How to summarize by group?, however, it seems that my data is a little different that makes things weird. I have a data.frame DF like so:
X Y1 Y2 Y3 Y4
3 A A B A
2 B B A A
1 B A A A
I want to make a sort of weighted sum of each unique factor in Y by its numeric value in X, such that the output is:
Y Y1 Y2 Y3 Y4
A 3 4 3 6
B 3 2 3 0
I had tried using a for loop to iterate over the indices of the columns, but I wasn't able to pass the number of the Y's correctly, and it didn't seem like the R way of doing this efficiently, for many more columns and rows.
It looks like according to the linked question, this is the right approach, however, when I try to extend to do the same across all the columns, via group_by and summarise_each, I get errors as the Y's are factors. Should I be using 'apply' instead? The logic of this seems straight forward but I've been stumped in its implementation.
aggregate(X~Y1,DF,sum)