在R中自动编码(总和)

时间:2018-10-02 15:06:56

标签: r sum

首先,如果我使用的术语不正确,我要道歉。

我有下面的数据集,其中包含各种各样的类别

以下是dput的摘录(使用droplevels)

structure(list(
x = c(2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L), *[ME: there are more years than 2010...]*
y = c(7.85986, 185.81068, 107.24097, 7094.74649, 
1.4982, 185.77319, 5090.79354, 167.58584, 4189.64609, 157.08277, 
3927.06932, 2.86732, 71.683, 4.70123, 117.53085, 2.93452, 73.36292, 
1.4982, 18.18734, 901.14744, 0.90268, 13.77532, 613.38298, 0.01845, 
0.0681, 7.19925, 3.75315, 0.14333, 136.54008, 0.04766, 0.59077, 
28.97255, 0.38608, 115.05258, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
x1 = structure(c(4L, 2L, 3L, 1L, 4L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 4L, 2L, 1L, 4L, 2L, 1L, 4L, 2L, 
1L, 2L, 4L, 1L, 4L, 2L, 1L, 4L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L), .Label = c("All greenhouse gases - (CO2 equivalent)", 
"CH4", "CO2", "N2O"), class = "factor"), 
x2 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Austria",         
class = "factor"), 
x4 = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 
10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 14L, 14L, 
15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L), .Label = c("3", 
"3.1", "3.A", "3.A.1", "3.A.2", "3.A.3", "3.A.4", "3.B", 
"3.B.1", "3.B.2", "3.B.3", "3.B.4", "3.B.5", "3.C", "3.C.1", 
"3.C.2", "3.C.3", "3.C.4"), class = "factor")), class = "data.frame",     
row.names = c(NA, 
-44L))

我想知道x4中子类别的总和(例如3.B.1 + 3.B.2 + ... + 3.Bn)是否等于父类别(例如3)中所述的数字。 B)。 (即csv中说明的总和)在给定的年份和国家/地区。我要核对这些款项。

要获得子类别的总和,我有这个

sum(df$y[df$x4 %in% c("3.A.1", "3.A.2", "3.A.3", "3.A.4") & x == 
"2010" & x2 == "Austria"])

要接收父类别的总和,我有这个

sum(df$y[df$x4 %in% c("3.A") & x == "2010" & x2 == "Austria"])

接下来,我将需要一个操作来检查两个代码的结果是否相等(真/假)。但是,我有20多个国家,20年的几十个类别要检查。使用我的newby方法,我将编写年龄很长的代码...

反正有自动化吗?基本上,我正在寻找能够执行以下操作的代码

1)运行一个类别,转到下一个类别 2)一旦完成类别更改年份,然后从类别重新开始 3)...对于国家/地区也一样。...

我们将不胜感激,甚至提供有关如何在标题中使用正确术语的建议。无论如何谢谢

1 个答案:

答案 0 :(得分:1)

这是使用dplyr的潜在解决方案(可能需要根据完整数据集进行一些调整):

require(dplyr)
# Create two columns - one that shows only the parent category number, and one that tells you if it's a parent or child; note that the regex here makes some assumptions on the format of your data.
mutate(df,parent=gsub("(.?\\..?)\\..*", "\\1", df$x4), 
  type=ifelse(parent==x4,"Parent","Child")) %>% 
# Sum the children y's by category, year and country
group_by(parent, type, x, x2) %>% 
summarize(sum(y)) %>% 
# See if the sum of the children is equal to the parent y
tidyr::spread(type,`sum(y)`) %>%
mutate(equals=isTRUE(all.equal(Child,Parent)))

使用(新)数据的结果:

  parent     x x2      Child Parent equals
  <chr>  <int> <fct>   <dbl>  <dbl> <lgl> 
1 3       2010 Austria   NA   7396. FALSE 
2 3.1     2010 Austria   NA   5278. FALSE 
3 3.A     2010 Austria 4357.  4357. TRUE  
4 3.B     2010 Austria  921.   921. TRUE  
5 3.C     2010 Austria    0      0  TRUE 

我从您的新数据中可以看到您有两个级别的父母。我的解决方案仅适用于第二级(例如3.1及其子级),但可以很容易地进行调整,使其也适用于最高级别。