I'm going to strongly recommend the tidyverse. Within that set of packages I would approach your problem like so:
library(tidyverse)
percent <-
database %>%
mutate(age_cat = case_when(
under_19 == 1 ~ "below 19",
under_19 == 0 ~ "over 19")) %>%
group_by(year, age_cat) %>%
summarise(count_ = n()) %>%
mutate(percent = count_/sum(count_))
percent %>%
ggplot(aes(x = year, y = percent, color = age_cat)) +
geom_point()
I am assuming that each row is an individual that you would like to count, and you want a summary of the percentage of rows in that year / age_cat grouping. You can also summarise by count_ = sum(insured)
. Take note that you may want to add the na.rm = TRUE
argument to your sum(insured)
if you anticipate and need to ignore NA
rows.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…