我正在尝试最后几个小时,以了解为什么我不能将另一个年龄组的年龄组重新分组?我正在使用R中的forecats库进行此操作。
这里是数据:
data <- structure(list(`Country code` = c(900, 900, 900, 900, 900, 900,
900, 900, 900, 900, 900, 900, 900, 900, 900, 900, 900, 900, 900,
900, 900), Type = c("World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World", "World", "World", "World", "World", "World",
"World", "World"), `Parent code` = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), year = c(2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020), age_grps = c("0-4",
"05-Sep", "Oct-14", "15-19", "20-24", "25-29", "30-34", "35-39",
"40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74",
"75-79", "80-84", "85-89", "90-94", "95-99", "100+"), value = c(" 677 942",
" 664 439", " 641 267", " 612 196", " 597 388", " 594 692",
" 605 531", " 544 819", " 493 789", " 479 366", " 445 773",
" 387 849", " 322 142", " 269 644", " 188 677", " 123 782",
" 81 930", " 42 186", " 16 680", " 4 134", " 573"
), age_groups = structure(c(1L, 2L, 21L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 3L
), .Label = c("0-4", "0-9", "100+", "15-19", "20-24", "25-29",
"30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64",
"65-69", "70-74", "75-79", "80-84", "85-89", "90-94", "95-99",
"10-14"), class = "factor")), row.names = c(NA, -21L), class = c("tbl_df",
"tbl", "data.frame"))
这是重新分类:
age_levels <- c("0-4" = "0-19",
"5-9" = "0-19",
"10-14" = "0-19",
"15-19" = "0-19",
"20-24" = "20-39",
"25-29" = "20-39",
"30-34" = "20-39",
"35-39" = "20-39",
"40-44" = "40-59",
"45-49" = "40-59",
"50-54" = "40-59",
"55-59" = "40-59",
"65+" = "60-64",
"65+" = "65-69",
"65+" = "70-74",
"65+" = "75-79",
"65+" = "80-84",
"65+" = "85-89",
"65+" = "90-94",
"65+" = "95-99",
"65+" = "100+")
这是我正在使用的代码:
data_rec <- data %>%
dplyr::mutate(age_recoded_band = forcats::fct_recode(age_groups, !!!age_levels))