在数据框的底部添加一行,该行对除了最后一行之外的所有先前行求和。

huangapple go评论80阅读模式
英文:

Add a row at the bottom of a dataframe that sums all previous rows but one

问题

  1. # 在R中在数据框的底部添加一行,该行对除了名为"Average"的行之外的所有行求和,并命名为"Sum"。
  2. df <- df %>%
  3. add_row(newname = "Sum",
  4. across(-newname, ~ifelse(newname == "Average", ., sum(.)), .names = "Average_{.col}")) %>%
  5. arrange(match(newname, c("Average", "Sum")))
英文:

I want add a new row at the bottom of a dataframe in R that sums all the rows but one and is called "Sum".

That is the data I am using. The row that I want to avoid summing is called "Average":

  1. structure(list(newname = c(&quot;Manufacturing&quot;, &quot;Hitech&quot;, &quot;Healthcare&quot;,
  2. &quot;Telecom&quot;, &quot;Consumer Non-Durables&quot;, &quot;Wholesale &amp; Retail&quot;, &quot;Average&quot;,
  3. &quot;Utilities&quot;, &quot;Services&quot;, &quot;Mining&quot;, &quot;Energy&quot;, &quot;Consumer Durables&quot;,
  4. &quot;Transportation&quot;, &quot;ConstructionRealState&quot;), Average_aqcflow2_1980 = c(0.0288997230442616,
  5. 0.00473850847901422, 0.00488971102785823, 0.00840323152275126,
  6. 0.0190959538907323, 0.0107096900100281, 0.00850912807485486,
  7. 0.00308284189592453, 0.004017425488436, 0.00460641845272515,
  8. 0.0114891264070224, 0.0064953691457694, 0.0035153434828485, 0.000675322125741407
  9. ), Average_aqcflow2_1990 = c(0.0269065869435805, 0.0135671840886106,
  10. 0.0111340963669521, 0.0215104218890732, 0.0122076623454889, 0.0116918032736543,
  11. 0.0104761191456682, 0.0163205965658905, 0.00819670938721455,
  12. 0.00402856998096242, 0.00209418113434955, 0.00605775671618596,
  13. 0.00198098445928488, 0.0004929957424395), Average_aqcflow2_2000 = c(0.0305917427040867,
  14. 0.0314788731282408, 0.0197484512800162, 0.0157122610991569, 0.0133167049007056,
  15. 0.0186446766062766, 0.01364500869393, 0.0133738286076718, 0.0104508681409246,
  16. 0.0137874110635133, 0.00275844908745865, 0.00307277644221122,
  17. 0.00347081132380885, 0.000978258637018894), Average_aqcflow2_2020 = c(0.0284892629360334,
  18. 0.0324692474550213, 0.027609908946151, 0.0135453695473018, 0.0103109824448211,
  19. 0.0112245533864386, 0.0117869672440038, 0.0101850560169536, 0.00672132360267508,
  20. 0.00642733960610653, 0.00145444978278222, 0.00188825357237733,
  21. 0.00225031095346272, 0.00065451592192456), Average_capxflow_1980 = c(0.643434694647758,
  22. 0.666385956120257, 0.521821601960193, 0.664079228180719, 0.469371963236091,
  23. 0.56521473192281, 0.715734653452937, 0.669312888488011, 0.761836358046314,
  24. 0.916619246090449, 0.838674518995832, 0.727230791846755, 0.996424567878545,
  25. 0.864143947474453), Average_capxflow_1990 = c(0.462312791721825,
  26. 0.538458058854539, 0.38163873310658, 0.606260403731961, 0.320447407002078,
  27. 0.580271668278576, 0.593077827558847, 0.511009223465175, 0.645892152746318,
  28. 0.985223788192333, 0.777112339777178, 0.560573099681717, 0.887464318925823,
  29. 0.453347772780902), Average_capxflow_2000 = c(0.312191039178838,
  30. 0.345003024008933, 0.281109104604407, 0.49848807485155, 0.240777157227638,
  31. 0.507584566582512, 0.492042373813665, 0.653762365929829, 0.584707714400257,
  32. 0.885795369682392, 0.535837944019047, 0.759871888687288, 0.733697427206183,
  33. 0.0577251831987745), Average_capxflow_2020 = c(0.26812620276161,
  34. 0.230589287727291, 0.168840100993732, 0.367314776341145, 0.207082273753817,
  35. 0.371645411190727, 0.592082324217154, 0.737529139398348, 0.486606395440387,
  36. 0.00606496464207505, 0.710208188236444, 3.32804243167463, 0.62278162517513,
  37. 0.192239417487673), Average_total_capxflow = c(0.427366078561936,
  38. 0.446852757298321, 0.337064381128767, 0.530237963233748, 0.312798672555739,
  39. 0.499445076760726, 0.610884432898405, 0.646195030225428, 0.617540074288915,
  40. 0.641802198428096, 0.737107062382711, 1.51194209148066, 0.810271303545411,
  41. 0.422874937788813), Average_total_aqcflow2 = c(0.0284824459575419,
  42. 0.0198130749738682, 0.0159434489103164, 0.0146198428839008, 0.0136166531654961,
  43. 0.012297650726387, 0.0108277645307654, 0.0103923542182047, 0.00693750973932877,
  44. 0.00637231107392752, 0.00450914450380519, 0.00441630096526052,
  45. 0.00269606140734965, 0.000664140374563966)), class = c(&quot;tbl_df&quot;,
  46. &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -14L))

I can't figure it out.. So far I have been using the package janitor but not sure if I can exclude a row:

  1. library(janitor)
  2. tablacash2 &lt;- tablacash22 %&gt;%
  3. adorn_totals(&quot;row&quot;)

答案1

得分: 1

基本方法

  1. tablacash2 = rbind(
  2. tablacash2,
  3. c("总计", colSums(tablacash2[df$newname != "平均", -1]))
  4. )

保持原始类型

  1. tablacash2 = rbind(
  2. tablacash2,
  3. c("newname" = "总计", as.list(colSums(tablacash2[tablacash2$newname != "平均", -1])))
  4. )
英文:

Base approach

  1. tablacash2=rbind(
  2. tablacash2,
  3. c(&quot;Sum&quot;,colSums(tablacash2[df$newname!=&quot;Average&quot;,-1]))
  4. )

to keep the original types

  1. tablacash2=rbind(
  2. tablacash2,
  3. c(&quot;newname&quot;=&quot;Sum&quot;,as.list(colSums(tablacash2[tablacash2$newname!=&quot;Average&quot;,-1])))
  4. )

答案2

得分: 0

一种方法是排除“平均”行,计算总和并将该行重新包含。

  1. 库(janitor)
  2. 库(dplyr)
  3. tablacash22 <- tablacash22 %>%
  4. mutate(rowindex = row_number())
  5. tablacash22 %>%
  6. filter(newname != "平均") %>%
  7. adorn_totals("row", name = "总和") %>%
  8. bind_rows(tablacash22 %>% filter(newname == "平均")) %>%
  9. arrange(rowindex) %>%
  10. select(-rowindex)

我们创建rowindex列以跟踪原始行的顺序。

英文:

One way would to exclude "Average" row, calculate the sum and include the row back.

  1. library(janitor)
  2. library(dplyr)
  3. tablacash22 &lt;- tablacash22 %&gt;% mutate(rowindex = row_number())
  4. tablacash22 %&gt;%
  5. filter(newname != &quot;Average&quot;) %&gt;%
  6. adorn_totals(&quot;row&quot;, name = &quot;Sum&quot;) %&gt;%
  7. bind_rows(tablacash22 %&gt;% filter(newname == &quot;Average&quot;)) %&gt;%
  8. arrange(rowindex) %&gt;%
  9. select(-rowindex)

We create rowindex column to keep track of original order of rows.

huangapple
  • 本文由 发表于 2023年5月29日 18:47:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定