2021年4月23日星期五

Explicit factor NAs in a data frame

I have the following data frame with ages binned in ranges of 5 years and the frequency of a condition happening in males/females. The problem is that there were no occurrences in either gender for example in the range 15-20.

structure(list(age = structure(c(1L, 2L, 3L, 5L, 6L, 7L, 8L,   9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L), .Label = c("[0,5]",   "(5,10]", "(10,15]", "(15,20]", "(20,25]", "(25,30]", "(30,35]",   "(35,40]", "(40,45]", "(45,50]", "(50,55]", "(55,60]", "(60,65]",   "(65,70]", "(70,75]", "(75,80]", "(80,85]", "(85,90]", "(90,95]",   "(95,100]"), class = "factor"), male = c(2L, 1L, 1L, 4L, 8L,   9L, 20L, 33L, 49L, 104L, 112L, 176L, 159L, 140L, 94L, 72L, 32L,   6L, 2L), female = c(1L, 1L, NA, 7L, 7L, 4L, 23L, 39L, 44L, 74L,   94L, 111L, 124L, 129L, 110L, 92L, 76L, 30L, 7L)), row.names = c(NA,   -19L), groups = structure(list(age = structure(c(1L, 2L, 3L,   5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,   19L, 20L), .Label = c("[0,5]", "(5,10]", "(10,15]", "(15,20]",   "(20,25]", "(25,30]", "(30,35]", "(35,40]", "(40,45]", "(45,50]",   "(50,55]", "(55,60]", "(60,65]", "(65,70]", "(70,75]", "(75,80]",   "(80,85]", "(85,90]", "(90,95]", "(95,100]"), class = "factor"),       .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,           10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L), ptype = integer(0), class =    c("vctrs_list_of",       "vctrs_vctr", "list"))), row.names = c(NA, -19L), class = c("tbl_df",   "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",   "tbl_df", "tbl", "data.frame"))  

If I check the levels it properly shows all levels. what I would want is a data frame where all ranges of ages show up and when they don't exist substitute then by 0.

https://stackoverflow.com/questions/67238707/explicit-factor-nas-in-a-data-frame April 24, 2021 at 10:06AM

没有评论:

发表评论