Hi,
I have this simple df
source <- data.frame(
serial = c(100001,100002,100008,100010,
100012,100013,100015,100017,100018,100019,100021,
100024,100026,100029,100033,100035,100036,100037,
100038),
educ = c(3, 2, 3, 3, 3, 1, 2, 3, 2, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3),
grads = c(1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1),
ethnic = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
wts = c(0.80453,1.61199,1.07317,
0.37436,0.50043,1.40756,1.86962,0.84807,0.94143,0.32848,
1.58918,0.30688,3.03342,0.973,0.42845,0.35284,1.07317,
0.94018,1.04452),
q5_1_Positivity = c(1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0),
q6_1_Positivity = c(1, 1, 0, 0, 1, 0, 1, 1, 1, NA, 1, 0, 1, 1, 1, NA, 1, 1, 0),
q6_2_Positivity = c(1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0),
q7_1_Positivity = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0),
q7_2_Positivity = c(1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0)
)
I would like to run following tables:
library(dplyr)
Rec.Pos <- source %>%
filter(!is.na(q5_1_Positivity)) %>%
mutate(q5_1=as.character(q5_1_Positivity)) %>%
group_by(q5_1_Positivity) %>%
summarise(cnt = n()) %>%
mutate(freq = round(cnt / sum(cnt), 3))
Rec.Pos
Posivity.educ <- source %>%
mutate(educ=as.character(educ)) %>%
group_by(educ) %>%
summarise_at(.vars = vars(ends_with(match = "Positivity")),.funs = list(Proportion = ~mean(.,na.rm=TRUE),Count = ~sum(!is.na(.))))
Posivity.educ
but with the weight (wts) applied. Can we do that in R?
Also, two additional questions:
- Is it possible to amend the second table to group not only by one variable (educ) but by educ, grads and ethic on a side (so 3 tables in one, one on top of another)
- Is it possible to see only one column with a count instead of 5 if they have identical values?