I am new to R and programming. I need to plot a dummy variable as a fraction of age groups. I have created the dummy and completed a count. How do I create a fraction of dummy per age group x?
data
meps_2013<-
meps_2013%>%
select(dupersid,age13x,ipdis13,sex)%>%
mutate(hospsD = ifelse(meps_2013$ipdis13 >= 1 & meps_2013$ipdis13 <= 9, 1, 0))
meps_2013
# A tibble: 36,940 × 3
dupersid age13x ipdis13
<dbl> <dbl> <dbl>
1 20004101 39 0
2 20004102 40 0
3 20004103 10 0
4 20005101 52 0
5 20005102 22 0
6 20005103 19 0
7 20006101 43 0
8 20006102 42 0
9 20006103 15 0
10 20006104 21 0
# … with 36,930 more rows
ipdis13 is variable used to create dummy.
Here is what I have:
Dummy variable: hospsD includes survey responses 1-9 equal to 1, 0 otherwise;
meps_2013<-
meps_2013%>%
select(dupersid,age13x,ipdis13,sex)%>%
mutate(hospsD = ifelse(meps_2013$ipdis13 >= 1 & meps_2013$ipdis13 <= 9, 1, 0))
meps_2013
no_hospD <- ifelse(meps_2013$ipdis13 == 0, 1, 0)
count(meps_2013, c("hospsD", "no_hospD"))
hospsD no_hospD freq
1 0 0 2
2 0 1 34694
3 1 0 2244
lm plot with error
summary_data <- meps_2013%>%
group_by(age13x)%>%
summarize(mean_hosps = mean(hospsD,na.rm=TRUE))
ggplot(summary_data, aes(x = age13x, y = mean_hosps)) +
geom_smooth(method = "lm") +
geom_point() +
labs(x="Age", y="Hospitalizations")
summary_data
Error in FUN(X[[i]], ...) : object 'age13x' not found
setDT(meps_2013)[, .(Frac = sum(hospsD == 1, na.rm = TRUE)), by = age13x][, Frac := Frac/sum(Frac)][]