I have a data frame that looks like this:
x y value weight
10 1 red 1
3.4 5 blue 2
10 10 blue 0.5
3 8 blue 0.5
3 8 red 4
10 1 blue 1
3 8 blue 2
3 8 blue 0.25
I want to recast it so that each row is a unique combination of "x" and "y", and the columns are the sums of the "weight"s for each different value of "value". If possible, I'd also like to have columns for raw counts of the values of "value". So for these data that would be:
x y red_count blue_count red_sum_of_weights blue_sum_of_weights
10 1 1 1 1 1
3.4 5 0 1 0 2
10 10 0 0 1 0.5
3 8 1 3 4 2.75
Is there a way to do this with reshape or reshape2? I can get it to do counts of the values with
dcast(data,x+y~value)
But I haven't been able to figure out how to get it to use the weights in the way I want it to. I need this to work with an arbitrary number of possible levels for value and an arbitrary different number of rows per x*y combination in the original dataset. I've written my own code just using for loops to do this, but it takes an extremely long time to run - so far has taken 6 hours to do the first 15% of a 600k row dataset, which isn't very practical! But I'm sure there must be a way to do this with existing functions?
Thanks very much in advance for any help!