合并行并将其值相加

时间:2013-05-14 04:42:39

标签: r aggregate

下面是我原始数据框的一小部分。我需要合并特定季节中重复特定id的行,其licvessel不同。通过组合我需要总和qttygrossTon

请以id 431中的season 1998为例(*)。

season   lic     id  qtty   vessel   grossTon
…   
1998    16350   431  40     435       57
1998    16353   431  28     303       22.54
…   

同一主题431有两个不同的lic(16350& 16353)和两个不同的vessels(435& 303)。在这个具体案例中预期的结果是:

  season     lic       id   qtty  vessel    grossTon
    …
    1998    16350      431   68     435     79.54
    …

我不介意在结果行中提醒哪个licvessel,我想要的是保持seasonid以及qtty的总和1}}和grossTon。在上面的示例中,我手动选择了lic 16350vessel 435

说实话,我不知道该怎么做,所以我非常感谢任何帮助。

谢谢

原始数据(* =要合并的行数)

season  lic id  qtty    vessel  grossTon
1998    15593   411 40  2643    31.5
1999    27271   411 40  2643    31.5
2000    35758   411 40  2643    31.5
2001    45047   411 50  2643    31.5
2002    56291   411 55  2643    31.5
2003    66991   411 55  2643    31.5
2004    80581   411 55  2643    31.5
2005    95058   411 52  NA  NA
2006    113379  411 50  10911   4.65
2007    120894  411 50  10911   4.65
2008    130033  411 50  2483    8.5
2009    139201  411 46  2296    50
2010    148833  411 46  2296    50
2011    158395  411 46  2296    50
1998    16350   431 40  435 57    # *
1998    16353   431 28  303 22.54 # *
2000    37491   436 50  2021    19.11
2001    47019   436 50  2021    19.11
2002    57588   436 51  2021    19.11
2003    69128   436 51  2021    19.11
2004    82400   436 52  2021    19.11
2005    95599   436 50  2021    19.11
2006    113126  436 50  2021    19.11
2007    122387  436 50  2021    19.11
2008    131126  436 50  2021    19.11
2009    140417  436 50  2021    19.11
2010    150673  436 50  2021    19.11
2011    159776  436 50  2021    19.11

另外,我需要保留每季只有一个id的上一行和后续行。像这样:(* =合并后产生的行)

 season lic id  qtty    vessel  grossTon
1998    15593   411 40  2643    31.5
1999    27271   411 40  2643    31.5
2000    35758   411 40  2643    31.5
2001    45047   411 50  2643    31.5
2002    56291   411 55  2643    31.5
2003    66991   411 55  2643    31.5
2004    80581   411 55  2643    31.5
2005    95058   411 52  NA  NA
2006    113379  411 50  10911   4.65
2007    120894  411 50  10911   4.65
2008    130033  411 50  2483    8.5
2009    139201  411 46  2296    50
2010    148833  411 46  2296    50
2011    158395  411 46  2296    50
1998    16350   431 68  435 79.54 #*
2000    37491   436 50  2021    19.11
2001    47019   436 50  2021    19.11
2002    57588   436 51  2021    19.11
2003    69128   436 51  2021    19.11
2004    82400   436 52  2021    19.11
2005    95599   436 50  2021    19.11
2006    113126  436 50  2021    19.11
2007    122387  436 50  2021    19.11
2008    131126  436 50  2021    19.11
2009    140417  436 50  2021    19.11
2010    150673  436 50  2021    19.11
2011    159776  436 50  2021    19.11

2 个答案:

答案 0 :(得分:11)

如果您将data.frame转换为data.table,您可以充分利用by参数

library(data.table)

DT <- data.table(DF)  # DF is your original data

然后它只是一行:

DT[, lapply(.SD, sum), by=list(season, lic, id, vessel)]

如果我们愿意,我们只能过滤1998 Season:'

DT[, lapply(.SD, sum), by=list(season, lic, id, vessel)][season==1998]
   season   lic  id vessel qtty grossTon
1:   1998 15593 411   2643   40    31.50
2:   1998 16350 431    435   68   114.00
3:   1998 16353 431    303   68    45.08

整个结果输出如下所示:

    season    lic  id vessel qtty grossTon
 1:   1998  15593 411   2643   40    31.50
 2:   1999  27271 411   2643   40    31.50
 3:   2000  35758 411   2643   40    31.50
 4:   2001  45047 411   2643   50    31.50
 5:   2002  56291 411   2643   55    31.50
 6:   2003  66991 411   2643   55    31.50
 7:   2004  80581 411   2643   55    31.50
 8:   2005  95058 411     NA   52       NA
 9:   2006 113379 411  10911   50     4.65
10:   2007 120894 411  10911   50     4.65
11:   2008 130033 411   2483   50     8.50
12:   2009 139201 411   2296   46    50.00
13:   2010 148833 411   2296   46    50.00
14:   2011 158395 411   2296   46    50.00
15:   1998  16350 431    435   68   114.00
16:   1998  16353 431    303   68    45.08
17:   1999  28641 431    303   68    45.08
18:   1999  28644 431    435   68   114.00
19:   2000  37491 436   2021   50    19.11
20:   2001  47019 436   2021   50    19.11
21:   2002  57588 436   2021   51    19.11
22:   2003  69128 436   2021   51    19.11
23:   2004  82400 436   2021   52    19.11
24:   2005  95599 436   2021   50    19.11
25:   2006 113126 436   2021   50    19.11
26:   2007 122387 436   2021   50    19.11
27:   2008 131126 436   2021   50    19.11
28:   2009 140417 436   2021   50    19.11
29:   2010 150673 436   2021   50    19.11
30:   2011 159776 436   2021   50    19.11
    season    lic  id vessel qtty grossTon

答案 1 :(得分:4)

这是一个单行基础解决方案,遵循Frank建议使用aggregate

Df_agg <- aggregate(. ~  season + lic + id + vessel, data = DF, sum)
# DF is your data
# we use season + lic + id + vessel as the grouping elements

检查输出:

Df_agg[with(Df_agg, order(lic)), ] 
# check the output (sort for convenience), identical to Ricardo Saporta's output
   season    lic  id vessel qtty grossTon
21   1998  15593 411   2643   40    31.50
3    1998  16350 431    435   68   114.00
1    1998  16353 431    303   68    45.08
22   1999  27271 411   2643   40    31.50
2    1999  28641 431    303   68    45.08
4    1999  28644 431    435   68   114.00
23   2000  35758 411   2643   40    31.50
5    2000  37491 436   2021   50    19.11
24   2001  45047 411   2643   50    31.50
6    2001  47019 436   2021   50    19.11
25   2002  56291 411   2643   55    31.50
7    2002  57588 436   2021   51    19.11
26   2003  66991 411   2643   55    31.50
8    2003  69128 436   2021   51    19.11
27   2004  80581 411   2643   55    31.50
9    2004  82400 436   2021   52    19.11
10   2005  95599 436   2021   50    19.11
11   2006 113126 436   2021   50    19.11
28   2006 113379 411  10911   50     4.65
29   2007 120894 411  10911   50     4.65
12   2007 122387 436   2021   50    19.11
20   2008 130033 411   2483   50     8.50
13   2008 131126 436   2021   50    19.11
17   2009 139201 411   2296   46    50.00
14   2009 140417 436   2021   50    19.11
18   2010 148833 411   2296   46    50.00
15   2010 150673 436   2021   50    19.11
19   2011 158395 411   2296   46    50.00
16   2011 159776 436   2021   50    19.11

检查1998年,与RS相同,似乎OP在所需输出中有误差,57 + 57!= 79.54但= = 114

Df_agg[Df_agg$season == 1998,]

   season   lic  id vessel qtty grossTon
21   1998 15593 411   2643   40    31.50
3    1998 16350 431    435   68   114.00
1    1998 16353 431    303   68    45.08