I have this table, and I want to reassign the case counts when the cause is C55. I want to redistribute it mathematically according to the proportion between C53 and C54 (that is, if both have 1, assign 50% of C55 to each). Always round down, and if there is any remaining whole number, assign it to C53. This should all be done separately for each age group.
# A tibble: 26 × 4
GENRE CAUSA GRUPEDAD CUENTA
<dbl> <chr> <chr> <dbl>
1 2 C55 55 a 59 1
2 2 C54 70 a 74 1
3 2 C54 80 y mas 1
4 2 C53 45 a 49 5
5 2 C54 60 a 64 1
6 2 C53 50 a 54 1
7 2 C53 80 y mas 2
8 2 C54 55 a 59 1
9 2 C53 65 a 69 3
10 2 C55 75 a 79 3
# ℹ 16 more rows
Here is the entire data set
dput(df)
structure(list(GENRE = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), CAUSA = c("C55", "C54",
"C54", "C53", "C54", "C53", "C53", "C54", "C53", "C55", "C53",
"C55", "C53", "C53", "C53", "C55", "C55", "C53", "C53", "C54",
"C55", "C55", "C55", "C53", "C55", "C55"), GRUPEDAD = c("55 a 59",
"70 a 74", "80 y mas", "45 a 49", "60 a 64", "50 a 54", "80 y mas",
"55 a 59", "65 a 69", "75 a 79", "55 a 59", "35 a 39", "60 a 64",
"40 a 44", "30 a 34", "70 a 74", "50 a 54", "70 a 74", "75 a 79",
"75 a 79", "40 a 44", "60 a 64", "45 a 49", "35 a 39", "65 a 69",
"80 y mas"), CUENTA = c(1, 1, 1, 5, 1, 1, 2, 1, 3, 3, 3, 2, 3,
3, 2, 3, 1, 4, 1, 1, 2, 1, 3, 3, 2, 5)), row.names = c(NA, -26L
), class = c("tbl_df", "tbl", "data.frame"))