0

I following the following post on github to create a occurence table of undernourishment indicators: https://github.com/DHSProgram/DHS-Indicators-R/commit/1c64b0f2af33ac56d149a7dbce34ea8bb21ce3f9

I struggling with that code (end of the Chap11_NT/NT_tables_PR.R file):

 table_temp <-  PRdata %>% 
  calc_cro_rpct(
    cell_vars = list(agemonths, hc27, hv025, hv024, hv270, total()),
    col_vars = list(nt_ch_sev_stunt, nt_ch_stunt, nt_ch_mean_haz, nt_ch_sev_wast, nt_ch_wast, 
                    nt_ch_ovwt_ht, nt_ch_mean_whz, nt_ch_sev_underwt, nt_ch_underwt, nt_ch_ovwt_age, nt_ch_mean_waz),
    weight = wt,
    total_label = "Weighted N",
    total_statistic = "w_cases",
    total_row_position = c("below"),
    expss_digits(digits=1)) %>%
  set_caption("Child's anthropometric indicators")

In my case I do not want to calculated the propositions to areas, but to the grids id (id column). More over I do have have several survey rounds (servrd). So my aim is to fit the code above so that my col_vars will be calculated for each grid on each servrd. (in my case PRdata = comb_extract_all): Two approches I tried:

table_temp <-  comb_extract_all %>% 
  cross_rpct(
    cell_vars = list(id, servrd),
    col_vars = list(nt_ch_sev_stunt, nt_ch_stunt, nt_ch_sev_wast, nt_ch_wast, 
                     nt_ch_sev_underwt, nt_ch_underwt),
    total_label = "Gridded N",
    total_statistic = "w_cases",
    total_row_position = c("below"),
    expss_digits(digits=1)) %>%
  set_caption("Child's anthropometric indicators")

gives me the misstake: Error: 'cro': all variables should be of the same length or length 1. I also tried an dplyr: groub_by (servrd) and in cell_vars only id but that just changed nothing (to do it without servrd).

The shorten dput of my data:

dput(comb_extract_all[1,1:36])
structure(list(CLUSTER = 1, SurveyId = "ET2005DHS", id = 869L, 
    hhid = "        1 32", hv001 = 1, hv002 = 32, hv005 = 1894033, 
    hv006 = 11, hv007 = 1997, hv021 = 1, hv023 = structure(5L, labels = structure(1:343, names = c("1", 
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
    "2", "20", "21", "22", "23", "24", "25", "3", "4", "5", "6", 
    "7", "8", "9", "Acholi", "Addis Ababa", "Afar-rural", "Afar-Urban", 
    "Affar rural", "Affar urban", "Amhara-rural", "Amhara-urban", 
    "Amhara rural", "Amhara urban", "Ankole", "Balaka - rural", 
    "Balaka - urban", "Benishangul-Gumuz-rural", "Benishangul-Gumuz-urban", 
    "Benishangul-Gumuz rural", "Benishangul-Gumuz urban", "Blantyre - rural", 
    "Blantyre - urban", "Bomi - Rural", "Bomi - Urban", "Bomi Rural", 
    "Bomi Urban", "Bong - Rural", "Bong - Urban", "Bong Rural", 
    "Bong Urban", "Bugesera", "Bugesera - rural", "Bugesera - urban", 
    "Bugisu", "Bukedi", "Bulawayo - Urban", "Bunyoro", "Burera", 
    "Burera - rural", "Burera - urban", "Busoga", "Central", 
    "Central 1", "Central 2", "Chikwawa - rural", "Chikwawa - urban", 
    "Chiradzulu - rural", "Chiradzulu - urban", "Chitipa - rural", 
    "Chitipa - urban", "Dedza - rural", "Dedza - urban", "Dire Dawa-urban", 
    "Dire Dawa -rural", "Dire Dawa rural", "Dire Dawa urban", 
    "Dowa - rural", "Dowa - urban", "East Central", "East Central Island-rural", 
    "Eastern", "Gakenke", "Gakenke - rural", "Gakenke - urban", 
    "Gambela-rural", "Gambela-urban", "Gambela rural", "Gambela urban", 
    "Gasabo", "Gasabo - rural", "Gasabo - urban", "Gatsibo", 
    "Gatsibo - rural", "Gatsibo - urban", "Gbarpolu - Rural", 
    "Gbarpolu - Urban", "Gbarpolu Rural", "Gbarpolu Urban", "Gicumbi", 
    "Gicumbi - rural", "Gicumbi - urban", "Gisagara", "Gisagara - rural", 
    "Gisagara - urban", "Grand Bassa - Rural", "Grand Bassa - Urban", 
    "Grand Bassa Rural", "Grand Bassa Urban", "Grand Cape Mount - Rural", 
    "Grand Cape Mount - Urban", "Grand Cape Mount Rural", "Grand Cape Mount Urban", 
    "Grand Gedeh - Rural", "Grand Gedeh - Urban", "Grand Gedeh Rural", 
    "Grand Gedeh Urban", "Grand Kru - Rural", "Grand Kru - Urban", 
    "Grand Kru Rural", "Grand Kru Urban", "Harare - Urban", "Harari-rural", 
    "Harari-urban", "Harari rural", "Harari urban", "Huye", "Huye - rural", 
    "Huye - urban", "Kamonyi", "Kamonyi - rural", "Kamonyi - urban", 
    "Kampala", "Karamoja", "Karonga - rural", "Karonga - urban", 
    "Karongi", "Karongi - rural", "Karongi - urban", "Kasungu - rural", 
    "Kasungu - urban", "Kayonza", "Kayonza - rural", "Kayonza - urban", 
    "Kicukiro", "Kicukiro - rural", "Kicukiro - urban", "Kigezi", 
    "Kirehe", "Kirehe - rural", "Kirehe - urban", "Lango", "Likoma - rural", 
    "Likoma - urban", "Lilongwe - rural", "Lilongwe - urban", 
    "Lofa - Rural", "Lofa - Urban", "Lofa Rural", "Lofa Urban", 
    "Machinga - rural", "Machinga - urban", "Mangochi - rural", 
    "Mangochi - urban", "Manicaland - Rural", "Manicaland - Urban", 
    "Margibi - Rural", "Margibi - Urban", "Margibi Rural", "Margibi Urban", 
    "Maryland - Rural", "Maryland - Urban", "Maryland Rural", 
    "Maryland Urban", "Mashonaland Central - Rural", "Mashonaland Central - Urban", 
    "Mashonaland East - Rural", "Mashonaland East - Urban", "Mashonaland West - Rural", 
    "Mashonaland West - Urban", "Masvingo - Rural", "Masvingo - Urban", 
    "Matabeleland North - Rural", "Matabeleland North - Urban", 
    "Matabeleland South - Rural", "Matabeleland South - Urban", 
    "Mchinji - rural", "Mchinji - urban", "Midlands - Rural", 
    "Midlands - Urban", "Montserrado - Rural", "Montserrado - Urban", 
    "Montserrado Rural", "Montserrado Urban", "Muhanga", "Muhanga - rural", 
    "Muhanga - urban", "Mulanje - rural", "Mulanje - urban", 
    "Musanze", "Musanze - rural", "Musanze - urban", "Mwanza - rural", 
    "Mwanza - urban", "Mzimba - rural", "Mzimba - urban", "National", 
    "Neno - rural", "Neno - urban", "Ngoma", "Ngoma - rural", 
    "Ngoma - urban", "Ngororero", "Ngororero - rural", "Ngororero - urban", 
    "Nimba - Rural", "Nimba - Urban", "Nimba Rural", "Nimba Urban", 
    "Nkhatabay - rural", "Nkhatabay - urban", "Nkhota kota - rural", 
    "Nkhota kota - urban", "North ( excluding refugee camps )", 
    "North Buganda", "North Buganda Island-rural", "North Buganda Island-urban", 
    "Northern", "Nsanje - rural", "Nsanje - urban", "Ntcheu - rural", 
    "Ntcheu - urban", "Ntchisi - rural", "Ntchisi - urban", "Nyabihu", 
    "Nyabihu - rural", "Nyabihu - urban", "Nyagatare", "Nyagatare - rural", 
    "Nyagatare - urban", "Nyamagabe", "Nyamagabe - rural", "Nyamagabe - urban", 
    "Nyamasheke", "Nyamasheke - rural", "Nyamasheke - urban", 
    "Nyanza", "Nyanza - rural", "Nyanza - urban", "Nyarugenge", 
    "Nyarugenge - rural", "Nyarugenge - urban", "Nyaruguru", 
    "Nyaruguru - rural", "Nyaruguru - urban", "Oromia-rural", 
    "Oromia-urban", "Oromiya rural", "Oromiya urban", "Phalombe - rural", 
    "Phalombe - urban", "Refugee camps", "River Cess - Rural", 
    "River Cess - Urban", "River Gee - Rural", "River Gee - Urban", 
    "River Gee Rural", "River Gee Urban", "Rivercess Rural", 
    "Rivercess Urban", "Rubavu", "Rubavu - rural", "Rubavu - urban", 
    "Ruhango", "Ruhango - rural", "Ruhango - urban", "Rulindo", 
    "Rulindo - rural", "Rulindo - urban", "Rumphi - rural", "Rumphi - urban", 
    "Rural : Harare", "Rural : Manicaland", "Rural : Mashonaland Central", 
    "Rural : Mashonaland East", "Rural : Mashonaland West", "Rural : Masvingo", 
    "Rural : Matabeleland North", "Rural : Matabeleland South", 
    "Rural : Midlands", "Rusizi", "Rusizi - rural", "Rusizi - urban", 
    "Rutsiro", "Rutsiro - rural", "Rutsiro - urban", "Rwamagana", 
    "Rwamagana - rural", "Rwamagana - urban", "S.N.N.P. rural", 
    "S.N.N.P. urban", "Salima - rural", "Salima - urban", "Sinoe - Rural", 
    "Sinoe - Urban", "Sinoe Rural", "Sinoe Urban", "SNNPR-rural", 
    "SNNPR-urban", "Somali-rural", "Somali-urban", "Somali oversample rural", 
    "Somali oversample urban", "Somali rural", "Somali urban", 
    "South Buganda", "South Buganda Island-rural", "South Buganda Island-urban", 
    "Southern", "Southwest", "Teso", "Thyolo - rural", "Thyolo - urban", 
    "Tigray-rural", "Tigray-urban", "Tigray rural", "Tigray urban", 
    "Tooro", "Urban : Bulawayo", "Urban : Harare", "Urban : Manicaland", 
    "Urban : Mashonaland Central", "Urban : Mashonaland East", 
    "Urban : Mashonaland West", "Urban : Masvingo", "Urban : Matabeleland North", 
    "Urban : Matabeleland South", "Urban : Midlands", "West Nile", 
    "Western", "Zomba - rural", "Zomba - urban")), class = c("haven_labelled", 
    "vctrs_vctr", "integer")), hv024 = structure(3L, labels = structure(1:66, names = c("Acholi", 
    "Addis Ababa", "Addis Adaba", "Afar", "Affar", "Amhara", 
    "Ankole", "Benishangul", "Benishangul-Gumuz", "Bugisu", "Bukedi", 
    "Bulawayo", "Bunyoro", "Busoga", "Central", "Central 1", 
    "Central 2", "Central region", "Dire Dawa", "East", "East Central", 
    "Eastern", "Gambela", "Harare", "Harari", "Kampala", "Karamoja", 
    "Kigali", "Kigali City", "Kigezi", "Lango", "Manicaland", 
    "Mashonaland Central", "Mashonaland East", "Mashonaland West", 
    "Masvingo", "Matabeleland North", "Matabeleland South", "Midlands", 
    "Monrovia", "North", "North Buganda", "North Central", "North Western", 
    "Northern", "Northern region", "Oromia", "Oromiya", "SNNP", 
    "SNNPR", "Somali", "South", "South Buganda", "South Central", 
    "South Eastern A", "South Eastern B", "Southern", "Southern region", 
    "Southwest", "Teso", "Tigray", "Tooro", "West", "West-Nile", 
    "West Nile", "Western")), class = c("haven_labelled", "vctrs_vctr", 
    "integer")), hv025 = structure(2, labels = c(Urban = 1, Rural = 2
    ), label = "Type of place of residence", class = c("haven_labelled", 
    "vctrs_vctr", "double")), hc1 = NA_real_, hc70 = structure(NA_real_, labels = c(`Height out of plausible limits` = 9996, 
    `Age in days out of plausible limits` = 9997, `Flagged cases` = 9998
    ), label = "Height/Age standard deviation (new WHO)", class = c("haven_labelled", 
    "vctrs_vctr", "double")), hc71 = structure(NA_real_, labels = c(`Height out of plausible limits` = 9996, 
    `Age in days out of plausible limits` = 9997, `Flagged cases` = 9998
    ), label = "Weight/Age standard deviation (new WHO)", class = c("haven_labelled", 
    "vctrs_vctr", "double")), hc72 = structure(NA_real_, labels = c(`Height out of plausible limits` = 9996, 
    `Age in days out of plausible limits` = 9997, `Flagged cases` = 9998
    ), label = "Weight/Height standard deviation (new WHO)", class = c("haven_labelled", 
    "vctrs_vctr", "double")), hc73 = structure(NA_real_, labels = c(`Height out of plausible limits` = 9996, 
    `Age in days out of plausible limits` = 9997, `Flagged cases` = 9998
    ), label = "BMI standard deviation (new WHO)", class = c("haven_labelled", 
    "vctrs_vctr", "double")), hv103 = structure(1, labels = c(No = 0, 
    Yes = 1), label = "Slept last night", class = c("haven_labelled", 
    "vctrs_vctr", "double")), ALT_DEM = 2314, LATNUM = 10.889096, 
    LONGNUM = 37.269565, ADM1NAME = "amhara", DHSREGNA = "amhara", 
    DATASET = "ETPR51sv", ISO = "ET", date = structure(12965, class = "Date"), 
    agemonths = structure(NA_real_, labels = c(`<6` = 1, `6-8` = 2, 
    `9-11` = 3, `12-17` = 4, `18-23` = 5, `24-35` = 6, `36-47` = 7, 
    `48-59` = 8), label = "Age of child months categories", class = c("haven_labelled", 
    "vctrs_vctr", "double")), geometry = structure(list(structure(list(
        list(structure(c(37.097223535328, 37.5404368343889, 37.4944179747303, 
        37.0516716693846, 37.097223535328, 11.2907418961895, 
        11.2445950330789, 10.80792545421, 10.8522375884668, 11.2907418961895
        ), dim = c(5L, 2L)))), class = c("XY", "MULTIPOLYGON", 
    "sfg"))), class = c("sfc_MULTIPOLYGON", "sfc"), precision = 0, bbox = structure(c(xmin = 37.0516716693846, 
    ymin = 10.80792545421, xmax = 37.5404368343889, ymax = 11.2907418961895
    ), class = "bbox"), crs = structure(list(input = "EPSG:4326", 
        wkt = "GEOGCRS[\"WGS 84\",\n    DATUM[\"World Geodetic System 1984\",\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L), 
    servrd = 1, nt_ch_stunt = structure(NA_real_, labels = c(Yes = 1, 
    No = 0), label = "Stunted child under 5 years", class = c("haven_labelled", 
    "vctrs_vctr", "double")), nt_ch_sev_wast = structure(NA_real_, labels = c(Yes = 1, 
    No = 0), label = "Severely wasted child under 5 years", class = c("haven_labelled", 
    "vctrs_vctr", "double")), nt_ch_wast = structure(NA_real_, labels = c(Yes = 1, 
    No = 0), label = "Wasted child under 5 years", class = c("haven_labelled", 
    "vctrs_vctr", "double")), nt_ch_sev_underwt = structure(NA_real_, labels = c(Yes = 1, 
    No = 0), label = "Severely underweight child under 5 years", class = c("haven_labelled", 
    "vctrs_vctr", "double")), nt_ch_underwt = structure(NA_real_, labels = c(Yes = 1, 
    No = 0), label = "Underweight child under 5 years", class = c("haven_labelled", 
    "vctrs_vctr", "double")), nt_ch_sev_stunt = structure(NA_real_, labels = c(Yes = 1, 
    No = 0), label = "Severely stunted child under 5 years", class = c("haven_labelled", 
    "vctrs_vctr", "double"))), row.names = 1L, class = "data.frame")
Sulz
  • 333
  • 1
  • 8

1 Answers1

0

You have expss_digits(digits=1) in the wrong place. It shouldn't be one of the arguments of the cross_rpct. Just put it on the distinct line.

Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • Interesting: Just copied that from here: https://github.com/DHSProgram/DHS-Indicators-R/commit/1c64b0f2af33ac56d149a7dbce34ea8bb21ce3f9 Actually I tried expss_digits on other lines, did't work (where exactly to put it?) I also don't quite see the solution to my problem to get like a table with the distinction first serrd and than id. – Sulz Dec 07 '22 at 08:59