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

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

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

问题

# 在R中在数据框的底部添加一行,该行对除了名为"Average"的行之外的所有行求和,并命名为"Sum"。
df <- df %>%
  add_row(newname = "Sum", 
          across(-newname, ~ifelse(newname == "Average", ., sum(.)), .names = "Average_{.col}")) %>%
  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":

structure(list(newname = c(&quot;Manufacturing&quot;, &quot;Hitech&quot;, &quot;Healthcare&quot;, 
&quot;Telecom&quot;, &quot;Consumer Non-Durables&quot;, &quot;Wholesale &amp; Retail&quot;, &quot;Average&quot;, 
&quot;Utilities&quot;, &quot;Services&quot;, &quot;Mining&quot;, &quot;Energy&quot;, &quot;Consumer Durables&quot;, 
&quot;Transportation&quot;, &quot;ConstructionRealState&quot;), Average_aqcflow2_1980 = c(0.0288997230442616, 
0.00473850847901422, 0.00488971102785823, 0.00840323152275126, 
0.0190959538907323, 0.0107096900100281, 0.00850912807485486, 
0.00308284189592453, 0.004017425488436, 0.00460641845272515, 
0.0114891264070224, 0.0064953691457694, 0.0035153434828485, 0.000675322125741407
), Average_aqcflow2_1990 = c(0.0269065869435805, 0.0135671840886106, 
0.0111340963669521, 0.0215104218890732, 0.0122076623454889, 0.0116918032736543, 
0.0104761191456682, 0.0163205965658905, 0.00819670938721455, 
0.00402856998096242, 0.00209418113434955, 0.00605775671618596, 
0.00198098445928488, 0.0004929957424395), Average_aqcflow2_2000 = c(0.0305917427040867, 
0.0314788731282408, 0.0197484512800162, 0.0157122610991569, 0.0133167049007056, 
0.0186446766062766, 0.01364500869393, 0.0133738286076718, 0.0104508681409246, 
0.0137874110635133, 0.00275844908745865, 0.00307277644221122, 
0.00347081132380885, 0.000978258637018894), Average_aqcflow2_2020 = c(0.0284892629360334, 
0.0324692474550213, 0.027609908946151, 0.0135453695473018, 0.0103109824448211, 
0.0112245533864386, 0.0117869672440038, 0.0101850560169536, 0.00672132360267508, 
0.00642733960610653, 0.00145444978278222, 0.00188825357237733, 
0.00225031095346272, 0.00065451592192456), Average_capxflow_1980 = c(0.643434694647758, 
0.666385956120257, 0.521821601960193, 0.664079228180719, 0.469371963236091, 
0.56521473192281, 0.715734653452937, 0.669312888488011, 0.761836358046314, 
0.916619246090449, 0.838674518995832, 0.727230791846755, 0.996424567878545, 
0.864143947474453), Average_capxflow_1990 = c(0.462312791721825, 
0.538458058854539, 0.38163873310658, 0.606260403731961, 0.320447407002078, 
0.580271668278576, 0.593077827558847, 0.511009223465175, 0.645892152746318, 
0.985223788192333, 0.777112339777178, 0.560573099681717, 0.887464318925823, 
0.453347772780902), Average_capxflow_2000 = c(0.312191039178838, 
0.345003024008933, 0.281109104604407, 0.49848807485155, 0.240777157227638, 
0.507584566582512, 0.492042373813665, 0.653762365929829, 0.584707714400257, 
0.885795369682392, 0.535837944019047, 0.759871888687288, 0.733697427206183, 
0.0577251831987745), Average_capxflow_2020 = c(0.26812620276161, 
0.230589287727291, 0.168840100993732, 0.367314776341145, 0.207082273753817, 
0.371645411190727, 0.592082324217154, 0.737529139398348, 0.486606395440387, 
0.00606496464207505, 0.710208188236444, 3.32804243167463, 0.62278162517513, 
0.192239417487673), Average_total_capxflow = c(0.427366078561936, 
0.446852757298321, 0.337064381128767, 0.530237963233748, 0.312798672555739, 
0.499445076760726, 0.610884432898405, 0.646195030225428, 0.617540074288915, 
0.641802198428096, 0.737107062382711, 1.51194209148066, 0.810271303545411, 
0.422874937788813), Average_total_aqcflow2 = c(0.0284824459575419, 
0.0198130749738682, 0.0159434489103164, 0.0146198428839008, 0.0136166531654961, 
0.012297650726387, 0.0108277645307654, 0.0103923542182047, 0.00693750973932877, 
0.00637231107392752, 0.00450914450380519, 0.00441630096526052, 
0.00269606140734965, 0.000664140374563966)), class = c(&quot;tbl_df&quot;, 
&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:

library(janitor)
tablacash2 &lt;- tablacash22 %&gt;%
  adorn_totals(&quot;row&quot;)

答案1

得分: 1

基本方法

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

保持原始类型

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

Base approach

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

to keep the original types

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

答案2

得分: 0

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

库(janitor)
库(dplyr)

tablacash22 <- tablacash22 %>%
  mutate(rowindex = row_number())

tablacash22 %>%
  filter(newname != "平均") %>%
  adorn_totals("row", name = "总和") %>%
  bind_rows(tablacash22 %>% filter(newname == "平均")) %>%
  arrange(rowindex) %>%
  select(-rowindex)

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

英文:

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

library(janitor)
library(dplyr)

tablacash22 &lt;- tablacash22 %&gt;% mutate(rowindex = row_number())

tablacash22 %&gt;%
  filter(newname != &quot;Average&quot;) %&gt;%
  adorn_totals(&quot;row&quot;, name = &quot;Sum&quot;) %&gt;%
  bind_rows(tablacash22 %&gt;% filter(newname == &quot;Average&quot;)) %&gt;%
  arrange(rowindex) %&gt;%
  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:

确定