I'm looking to create a frequency table in R for a certain variable (INTERVIEW_DAY) , but taking into account another variable as weight (WEIGHT).
I've tried to do that with the package data.table
. I would love to do it with the R-Base Package though.
Below you'll find the type of table I want, but still not weighted, which is what I'm looking to learn.
Data (variables TUCASEID, INTERVIEW_DAY, and WEIGHT):
TUCASEID INTERVIEW_DAY WEIGHT
1 2.00301e+13 5 8155462.7
2 2.00301e+13 6 1735322.5
3 2.00301e+13 6 3830527.5
4 2.00301e+13 4 6622023.0
5 2.00301e+13 4 3068387.3
6 2.00301e+13 4 3455424.9
7 2.00301e+13 1 1637826.3
8 2.00301e+13 2 6574426.8
9 2.00301e+13 6 1528296.3
10 2.00301e+13 4 4277052.8
11 2.00301e+13 6 1961482.3
12 2.00301e+13 7 505227.2
13 2.00301e+13 6 2135476.8
14 2.00301e+13 3 5366309.3
15 2.00301e+13 6 1058351.1
Creating table with the packaged data.table:
df <- setDT(df)
df_freq_table <- df[,.(Freq = .N), by = INTERVIEW_DAY][, Prop := Freq / sum(Freq)][, Cum := cumsum(100 * Prop / sum(Prop))]
My output: df_freq_table[]
INTERVIEW_DAY Freq Prop Cum
1: 5 1 0.06666667 6.666667
2: 6 6 0.40000000 46.666667
3: 4 4 0.26666667 73.333333
4: 1 1 0.06666667 80.000000
5: 2 1 0.06666667 86.666667
6: 7 1 0.06666667 93.333333
7: 3 1 0.06666667 100.000000