rdplyrjanitor

How to solve error when using adorn_totals function in R?


I get the following message of error when using janitor::adorn_totals("row"):

"Error in adorn_totals(., "row") : trying to re-add a totals dimension that is already been added"

Here is the head of my dataset :

structure(list(code_1 = c("M01", "C03", "M99", "C05", "O01", 
"C07"), regroupement_elsan = c("Gastro", "Ophtalmo", "Divers médecine", 
"Gynéco", "Accouchements", "bouche et dents"), actes_2019 = c(9179, 
5589, 6024, 4150, 4028, 3458), actes_2020 = c(7933, 4167, 3740, 
2994, 3348, 2206), actes_2021 = c(6504, 5505, 4682, 3376, 3226, 
3035), sejours_2019 = c(1631, 2502, 1028, 852, 1455, 1288), sejours_2020 = c(1335, 
1819, 726, 574, 1371, 801), sejours_2021 = c(1109, 2416, 825, 
657, 1259, 1106), tx_0_nuit_2019 = c("3.92397302268547", "90.7673860911271", 
"32.9766536964981", "57.5117370892019", "0.206185567010309", 
"98.9130434782609"), tx_0_nuit_2020 = c("3.29588014981273", "92.9081913139087", 
"47.1074380165289", "59.581881533101", "0.291757840991977", "99.250936329588"
), tx_0_nuit_2021 = c("3.6068530207394", "95.4470198675497", 
"18.3030303030303", "60.2739726027397", "0.158856235107228", 
"98.7341772151899"), pourcentage = c(5.37796226165473, 4.55191916519208, 
3.87140518282095, 2.79151300666457, 2.66748251170021, 2.50955034811226
), pourcentage_cumule = c(78.4062908267046, 82.9582099918967, 
86.8296151747176, 89.6211281813822, 92.2886106930824, 94.7981610411947
)), row.names = c(NA, -6L), class = c("tabyl", "tbl_df", "tbl", 
"data.frame"), core = structure(list(code_1 = c("M01b", "C01", 
"C02", "C04", "M01", "C03", "M99", "C05", "O01", "C07", "C08", 
"C99", "C98", "C10", "C06", "M03", "O02", "M02", "M04", "C01b", 
"O03", "S99", "***", "C10b", "M05", "M98", "O04"), regroupement_elsan = c("Endoscopies 
 digestives", 
"Ortho (+ rhumato et rachis)", "Chirurgie digestive", "Uro-néphro", 
"Gastro", "Ophtalmo", "Divers médecine", "Gynéco", "Accouchements", 
"bouche et dents", "Tissus mou et chir plastique", "Divers chir", 
"Chir esth et hors sécu", "Chir thoracique et vasculaire", "ORL Stomato sf bouche et 
 dent", 
"Pneumologie", "Obstétrique autre (hors IVG)", "Cardio Vasc (médecine)", 
"Neurologie", "Rachis", "IVG", "Séances autres", "Autres", "Chir thoracique", 
"Soins palliatifs", "Vasculaire interventionnel", "Néo nat"), 
    actes_2019 = c(36079, 29520, 14618, 6515, 9179, 5589, 6024, 
    4150, 4028, 3458, 2137, 2180, 575, 449, 866, 388, 294, 311, 
    714, 395, 292, 1842, 10, 0, 4, 0, 1), actes_2020 = c(30192, 
    25451, 12845, 7376, 7933, 4167, 3740, 2994, 3348, 2206, 2107, 
    1477, 575, 437, 337, 897, 193, 218, 267, 308, 118, 737, 8, 
    4, 0, 11, 5), actes_2021 = c(42333, 24055, 13735, 8196, 6504, 
    5505, 4682, 3376, 3226, 3035, 2571, 1134, 689, 511, 352, 
    272, 181, 161, 138, 106, 82, 61, 18, 8, 7, 0, 0), sejours_2019 = c(6992, 
    5493, 2577, 1221, 1631, 2502, 1028, 852, 1455, 1288, 540, 
    397, 236, 158, 260, 63, 148, 101, 90, 44, 246, 1820, 4, 0, 
    1, 0, 1), sejours_2020 = c(5811, 4946, 2220, 1220, 1335, 
    1819, 726, 574, 1371, 801, 554, 269, 221, 140, 94, 42, 109, 
    79, 58, 34, 98, 720, 2, 1, 0, 1, 5), sejours_2021 = c(7922, 
    5144, 2523, 1451, 1109, 2416, 825, 657, 1259, 1106, 649, 
    264, 278, 162, 111, 51, 108, 69, 30, 21, 77, 54, 7, 1, 2, 
    0, 0), tx_0_nuit_2019 = c("96.0955377574371", "63.5718186783179", 
    "41.4435389988359", "36.2817362817363", "3.92397302268547", 
    "90.7673860911271", "32.9766536964981", "57.5117370892019", 
    "0.206185567010309", "98.9130434782609", "72.5925925925926", 
    "53.904282115869", "13.9830508474576", "96.2025316455696", 
    "50.7692307692308", "42.8571428571429", "85.1351351351351", 
    "72.2772277227723", "11.1111111111111", "4.54545454545455", 
    "100,0", "100,0", "100,0", "0,0", "0,0", "0,0", "0,0"), tx_0_nuit_2020 = 
c("96.0936155567028", 
    "67.3069146785281", "40.5855855855856", "34.344262295082", 
    "3.29588014981273", "92.9081913139087", "47.1074380165289", 
    "59.581881533101", "0.291757840991977", "99.250936329588", 
    "76.3537906137184", "49.814126394052", "11.7647058823529", 
    "99.2857142857143", "53.1914893617021", "16.6666666666667", 
    "74.3119266055046", "81.0126582278481", "25.8620689655172", 
    "8.82352941176471", "98.9795918367347", "100,0", "100,0", 
    "100,0", "0,0", "0,0", "20,0"), tx_0_nuit_2021 = c("96.7053774299419", 
    "73.2892690513219", "51.0503369005153", "41.9021364576154", 
    "3.6068530207394", "95.4470198675497", "18.3030303030303", 
    "60.2739726027397", "0.158856235107228", "98.7341772151899", 
    "83.9753466872111", "60.2272727272727", "50,0", "94.4444444444444", 
    "72.972972972973", "1.96078431372549", "81.4814814814815", 
    "85.5072463768116", "43.3333333333333", "52.3809523809524", 
    "100,0", "100,0", "100,0", "100,0", "0,0", "0,0", "0,0")), row.names = c(NA, 
-27L), class = "data.frame"), tabyl_type = "two_way", totals = "row")

And the code I tried :

library(janitor)
autres %>%
  adorn_totals("row")

Could anyone help ? I had indeed used the adorn_totals function on the dataframe used to generate the dataframe "autres", but I made sure the row "total" isn't in the dataframe "autres" anymore.


Solution

  • With the object you have shared as x:

    x %>%
      untabyl() %>%
      adorn_totals()
    

    Why it works:

    You can see at the end of the object you shared, tabyl_type = "two_way", totals = "row". Those attributes are stored with the data.frame you're working with. When you try to adorn_totals() a second time, janitor checks this and errors.

    When you call untabyl() it strips those attributes. Then adorn_totals() succeeds.

    I notice you have a cumulative percentage column. If desired, you can control exactly which columns get a totals value in adorn_totals() - see ?adorn_totals and the ... argument for how, and here's an example: https://stackoverflow.com/a/69759313.