英文:
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("Manufacturing", "Hitech", "Healthcare",
"Telecom", "Consumer Non-Durables", "Wholesale & Retail", "Average",
"Utilities", "Services", "Mining", "Energy", "Consumer Durables",
"Transportation", "ConstructionRealState"), 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("tbl_df",
"tbl", "data.frame"), 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 <- tablacash22 %>%
adorn_totals("row")
答案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("Sum",colSums(tablacash2[df$newname!="Average",-1]))
)
to keep the original types
tablacash2=rbind(
tablacash2,
c("newname"="Sum",as.list(colSums(tablacash2[tablacash2$newname!="Average",-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 <- tablacash22 %>% mutate(rowindex = row_number())
tablacash22 %>%
filter(newname != "Average") %>%
adorn_totals("row", name = "Sum") %>%
bind_rows(tablacash22 %>% filter(newname == "Average")) %>%
arrange(rowindex) %>%
select(-rowindex)
We create rowindex
column to keep track of original order of rows.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论