r - Proportion Table by Group by Year (data table) of Unique Observations -


i have table unique {id, product type}, , runs year:

id product_type sex year

1 f 2000

1 b f 2000

1 b f 2001

1 m 2000

1 b m 2000

1 b m 2001

etc.

i proportion table of sex year (% of male , female customers year).

this tried,

library(data.table) dt <- data.table(salesdata) dt[, .(distincts = length(unique(id))), by=list(year,sex)] 

and gives me count of gender year. how can percentages or proportions of males , females year?

try this:

gmodels::crosstable(dt$sex, dt$year, prop.t = f, prop.chisq = f)     cell contents |-------------------------| |                       n | |           n / row total | |           n / col total | |-------------------------|   total observations in table:  6                 | dt$year        dt$sex |      2000 |      2001 | row total |  -------------|-----------|-----------|-----------|            f |         2 |         1 |         3 |               |     0.667 |     0.333 |     0.500 |               |     0.500 |     0.500 |           |  -------------|-----------|-----------|-----------|            m |         2 |         1 |         3 |               |     0.667 |     0.333 |     0.500 |               |     0.500 |     0.500 |           |  -------------|-----------|-----------|-----------| column total |         4 |         2 |         6 |               |     0.667 |     0.333 |           |  -------------|-----------|-----------|-----------| 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -