Here is some sample code:
dat = data.frame(income = c(100,200,300,400,500,600),
sex = c("M","M","M", "F","F","F"),
num.kid = c(1,2,3,1,2,3))
I want to produce a 2-dimensional table that summarizes the key statistics (e.g. mean and var) of income distribution by sex and num.kid.
For example, table(dat$sex, dat$num.kid)
would give me a 2x3 table with sex
as rows and num.kid
as columns, but the table would be filled with the count of those combinations. How can I bring a third variable (e.g. income) into the table? How can I fill the table with mean or var of income
by sex
and num.kid
? This is almost like filling out an Excel pivot table using R code.