如何解决在R中使用adorn_totals函数时出现的错误?

3
当我使用janitor::adorn_totals("row")时,会出现以下错误信息:

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

这是我的数据集的头部:
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")

我尝试的代码:

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

有人可以帮忙吗?我确实在用来生成数据框“autres”的数据框上使用了adorn_totals函数,但我确保行“total”不再存在于数据框“autres”中。

1个回答

3
使用您分享的对象 x
x %>%
  untabyl() %>%
  adorn_totals()

为什么它有效:

您可以在共享的对象末尾看到tabyl_type =“two_way”,totals =“row”。 这些属性与您正在使用的data.frame一起存储。 当您尝试第二次adorn_totals()时,janitor会检查并报错。

当您调用untabyl()时,它会剥离这些属性。 然后adorn_totals()就会成功。

我注意到您有一个累积百分比列。 如果需要,在adorn_totals()中可以精确控制哪些列获得总计值-请参见?adorn_totals...参数的方式,并在此处查看示例:https://dev59.com/MMLra4cB1Zd3GeqPMJDs#69759313


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接