1

I have a dataframe with three vectors:

location size  sales
A          1    100
A          1    300
A          2    200
A          3    300
B          1    300
B          1    500
C          1    800
C          1    900
B          3    500
B          3    600

I want to output a matrix with location on the X axis, size on the Y axis and mean sales of each (location, size) coordinate in the cells (sorted ascending on each axis).

   A   B    C
1 200 400   850
2 200     
3 300 550
...

I am new to R. I think I need to use the aggregate function. Thank you!

Jaap
  • 77,147
  • 31
  • 174
  • 185
rhk123
  • 29
  • 5

1 Answers1

1

We can use dcast

library(data.table)
dcast(setDT(df1), size~location, value.var="sales", mean)
akrun
  • 789,025
  • 32
  • 460
  • 575