0

Here is a toy data.frame:

sex = c(rep("M", 4), rep("F", 4))
bg = c(rep("W",2),rep("NW",2),rep("W",2),rep("NW",2))
income = c(156,185,105,115,95,78,67,74)
(data = data.frame(income, sex, bg))

  income sex bg
1    156   M  W
2    185   M  W
3    105   M NW
4    115   M NW
5     95   F  W
6     78   F  W
7     67   F NW
8     74   F NW

I am looking for a way to turn this object into a cross-table with, for example, the mean in every cell:

enter image description here

Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114

1 Answers1

1

You can do this using this code:

library(reshape2)

dcast(df,bg ~ sex,fun.aggregate = mean,value.var='income')

##  bg    F     M
##1 NW 70.5 110.0
##2  W 86.5 170.5
Kunal Puri
  • 3,419
  • 1
  • 10
  • 22