Try
library(reshape2) acast(diamonds, cut~color, value.var='price', mean) # DEFGHIJ #Fair 4291.061 3682.312 3827.003 4239.255 5135.683 4685.446 4975.655 #Good 3405.382 3423.644 3495.750 4123.482 4276.255 5078.533 4574.173 #Very Good 3470.467 3214.652 3778.820 3872.754 4535.390 5255.880 5103.513 #Premium 3631.293 3538.914 4324.890 4500.742 5216.707 5946.181 6294.592 #Ideal 2629.095 2597.550 3374.939 3720.706 3889.335 4451.970 4918.186
Or using base R
with(diamonds, tapply(price, list(cut,color), FUN= mean))
Or as @DavidArenburg suggested
xtabs(price ~ cut + color, diamonds)/table(diamonds[c('cut', 'color')])
You can also try dcast from devel data.table ie v1.9.5.
library(data.table) dcast(as.data.table(diamonds), cut~color, value.var='price', mean)
If the grouping variables are “clarity” and “color”
with(diamonds, tapply(price, list(clarity,color), FUN = mean))
For other functions, change FUN to tapply or fun.aggregate to acast/dcast
akrun
source share