如何创建,然后插入新行总和计算一个循环?

问题描述 投票:2回答:2

我试图找到通过类似主题的解决方案,但还没有找到什么合适。这可能是因为我已经使用的搜索字词。如果我错过了一些东西,请接受我的道歉。

这是我的数据UN_(所提供的样品应足够)的摘录:

 country year sector       UN 
      AT 1990      1 1.407555  
      AT 1990      2 1.037137  
      AT 1990      3 4.769618  
      AT 1990      4 2.455139  
      AT 1990      5 2.238618  
      AT 1990  Total 7.869005  
      AT 1991      1 1.484667  
      AT 1991      2 1.001578  
      AT 1991      3 4.625927  
      AT 1991      4 2.515453 
      AT 1991      5 2.702081 
      AT 1991  Total 8.249567 
      ....
      BE 1994      1 3.008115  
      BE 1994      2 1.550344  
      BE 1994      3 1.080667  
      BE 1994      4 1.768645  
      BE 1994      5 7.208295  
      BE 1994  Total 1.526016  
      BE 1995      1 2.958820  
      BE 1995      2 1.571759 
      BE 1995      3 1.116049  
      BE 1995      4 1.888952
      BE 1995      5 7.654881  
      BE 1995  Total 1.547446 
      ....

我想要做的是,要添加其他行与UN_$sector = Residual。剩余的价值将是(UN_$sector = Total) - (the sum of column UN for the sectors c("1", "2", "3", "4", "5"))对于给定年份和国家。

这是应该的样子:

 country year      sector       UN 
      AT 1990           1 1.407555  
      AT 1990           2 1.037137  
      AT 1990           3 4.769618  
      AT 1990           4 2.455139  
      AT 1990           5 2.238618  
----> AT 1990    Residual TO BE CALCULATED
      AT 1990       Total 7.869005 

因为我不想写很多代码,多行我正在寻找一种方式来自动完成这个。我被告知循环,但不能真正遵循此刻的概念。

非常感谢您对任何类型的帮助!

最好,

康斯坦丁

PS:(完美)

   country year sector        UN ETS
   UK      2012      1 190336512  NA
   UK      2012      2  18107910  NA
   UK      2012      3   8333564  NA
   UK      2012      4  11269017  NA
   UK      2012      5   2504751  NA
   UK      2012  Total 580957306  NA
   UK      2013      1 177882200  NA
   UK      2013      2  20353347  NA
   UK      2013      3   8838575  NA
   UK      2013      4  11051398  NA
   UK      2013      5   2684909  NA
   UK      2013  Total 566322778  NA
r loops sum
2个回答
0
投票

考虑计算残余第一,然后用数据的其他部分将其叠:

# CALCULATE RESIDUALS BY MERGED COLUMNS
agg <- within(merge(aggregate(UN ~ country + year, data = subset(df, sector!='Total'), sum),
                    aggregate(UN ~ country + year, data = subset(df, sector=='Total'), sum),
                    by=c("country", "year")),
             {UN <- UN.y - UN.x
              sector = 'Residual'})

# ROW BIND DIFFERENT PIECES
final_df <- rbind(subset(df, sector!='Total'),
                  agg[c("country", "year", "sector", "UN")],
                  subset(df, sector=='Total'))

# ORDER ROWS AND RESET ROWNAMES
final_df <- with(final_df, final_df[order(country, year, as.character(sector)),])
row.names(final_df) <- NULL

Rextester demo

final_df
#    country year   sector         UN
# 1       AT 1990        1   1.407555
# 2       AT 1990        2   1.037137
# 3       AT 1990        3   4.769618
# 4       AT 1990        4   2.455139
# 5       AT 1990        5   2.238618
# 6       AT 1990 Residual  -4.039062
# 7       AT 1990    Total   7.869005
# 8       AT 1991        1   1.484667
# 9       AT 1991        2   1.001578
# 10      AT 1991        3   4.625927
# 11      AT 1991        4   2.515453
# 12      AT 1991        5   2.702081
# 13      AT 1991 Residual  -4.080139
# 14      AT 1991    Total   8.249567
# 15      BE 1994        1   3.008115
# 16      BE 1994        2   1.550344
# 17      BE 1994        3   1.080667
# 18      BE 1994        4   1.768645
# 19      BE 1994        5   7.208295
# 20      BE 1994 Residual -13.090050
# 21      BE 1994    Total   1.526016
# 22      BE 1995        1   2.958820
# 23      BE 1995        2   1.571759
# 24      BE 1995        3   1.116049
# 25      BE 1995        4   1.888952
# 26      BE 1995        5   7.654881
# 27      BE 1995 Residual -13.643015
# 28      BE 1995    Total   1.547446

0
投票

我认为,有多种方法可以做到这一点。我可以建议是采取包的tidyverse套件,其中包括dplyr的优势。

没有得到太多的远成什么dplyrtidyverse可以实现,我们可以谈论dplyr的直列命令group_by(...)summarise(...)arrange(...)bind_rows(...)功能的力量。此外,也有很多很好的教程,作弊表和文档的所有tidyverse包。

虽然它是越来越少有关,这些天,我们一般要避免对R.循环。因此,我们将创建一个包含所有的剩余价值的,然后把它放回原来的数据帧新的数据帧。

步骤1:计算所有残余值

我们希望联合国的计算值的总和,通过countryyear分组。我们可以通过这个值达到这个

res_UN = UN_ %>% group_by(country, year) %>% summarise(UN = sum(UN, na.rm = T))

步骤2:扇区列res_UN添加具有值“残留”

这应该产生包含countryyearUN数据帧,我们现在需要添加一列sector值“剩余”,以满足您的要求它。

res_UN$sector = 'Residual'

第3步:添加res_UN回到UN_和订单相应

res_UNUN_现在有相同的列,现在他们可以将其合并。

UN_ = bind_rows(UN_, res_UN) %>% arrange(country, year, sector)

这个拼凑一起,应该回答你的问题,并且可以在几行可以实现!

TLDR:

res_UN = UN_ %>% group_by(country, year) %>% summarise(UN = sum(UN, na.rm = T))`
res_UN$sector = 'Residual'
UN_ = bind_rows(UN_, res_UN) %>% arrange(country, year, sector)
© www.soinside.com 2019 - 2024. All rights reserved.