How to group and count lines in a database in R?

Asked

Viewed 40 times

0

  • Please edit the question to limit it to a specific problem with sufficient detail to identify an appropriate answer.

1 answer

2


First the totals of pregnant women per CODMUNRES and by age group.

idades <- c(0, 16, 18, 23, 30, 36, Inf)
labels <- c("<16", "17-18", "19-23", "24-30", "31-36", ">36")

tbl <- table(dados$CODMUNRES)
top6 <- head(sort(tbl, decreasing = TRUE), n = 6)
i_top6 <- which(dados$CODMUNRES %in% names(top6))

IDADE <- cut(dados[i_top6, "IDADEMAE"], breaks = idades, labels = labels)
agg <- aggregate(GRAVIDEZ ~ CODMUNRES + IDADE, dados[i_top6, ], length)

head(agg)
#  CODMUNRES IDADE GRAVIDEZ
#1    210140   <16       91
#2    210300   <16      159
#3    210530   <16      166
#4    211120   <16      121
#5    211130   <16      354
#6    211220   <16      164

Now the average of pregnancies by age in these 6 codes.

aggregate(GRAVIDEZ ~ IDADE, agg, mean)
#  IDADE  GRAVIDEZ
#1   <16  175.8333
#2 17-18  295.1667
#3 19-23 1044.1667
#4 24-30 1363.8333
#5 31-36  851.5000
#6   >36  319.0000

Browser other questions tagged

You are not signed in. Login or sign up in order to post.