2013-08-12 18 views
1

我有一个大的数据框,其中某些行在其某些列中有重复的值。我想保留重复的值并对不同的值进行求和。下面是我的数据样本:将某些行和列的单元格求和

data<-data.frame(season=c(2008,2009,2010,2011,2011,2012,2000,2001), 
      lic=c(132228,140610,149215,158559,158559,944907,37667,45724), 
      client=c(174,174,174,174,174,174,175,175), 
      qtty=c(31,31,31,31,31,31,36,26), 
      held=c(60,65,58,68,68,70,29,23), 
      catch=c(7904,6761,9236,9323.2,801,NA,2330,3594.5), 
      potlift=c(2715,2218,3000,3887,750,NA,2314,3472)) 

season lic client qtty held catch potlift 
2008 132228 174 31 60 7904 2715 
2009 140610 174 31 65 6761 2218 
2010 149215 174 31 58 9236 3000 
2011 158559 174 31 68 9323.2 3887 
2011 158559 174 31 68 801 750 
2012 944907 174 31 70 NA NA 
2000 37667 175 36 29 2330 2314 
2001 45724 175 26 23 3594.5 3472 

注意,季节2011被重复,每个变量(client... held),除catchpotlift。我需要保留(client... held)和总和catchpotlift的值;因此,我的新的数据帧应该像下面的例子:

season lic client qtty held catch potlift 
2008 132228 174 31 60 7904 2715 
2009 140610 174 31 65 6761 2218 
2010 149215 174 31 58 9236 3000 
2011 158559 174 31 68 10124.2 4637 
2012 944907 174 31 70 NA NA 
2000 37667 175 36 29 2330 2314 
2001 45724 175 26 23 3594.5 3472 

我试图这样做使用aggregate,不过这个功能和一切。任何帮助将不胜感激。

回答

2
data$catch <- with(data, ave(catch,list(lic,client,qtty,held),FUN=sum)) 
data$potlift <- with(data, ave(potlift,list(lic,client,qtty,held),FUN=sum)) 
unique(data) 
    season lic client qtty held catch potlift 
1 2008 132228 174 31 60 7904.0 2715 
2 2009 140610 174 31 65 6761.0 2218 
3 2010 149215 174 31 58 9236.0 3000 
4 2011 158559 174 31 68 10124.2 4637 
6 2012 944907 174 31 70  NA  NA 
7 2000 37667 175 36 29 2330.0 2314 
8 2001 45724 175 26 23 3594.5 3472 
+0

您好托马斯,非常感谢! – Rafael

+0

通常,我发现将它们粘贴在'内''或'transform',当我使用'ave'来做多个“聚合”时,我发现'aggregate'在这个数据集上工作得很好! – A5C1D2H2I1M1N2O1R2T1

2

aggregate似乎为我工作得很好,但我不知道你试图什么:

> aggregate(cbind(catch, potlift) ~ ., data, sum, na.action = "na.pass") 
    season lic client qtty held catch potlift 
1 2001 45724 175 26 23 3594.5 3472 
2 2000 37667 175 36 29 2330.0 2314 
3 2010 149215 174 31 58 9236.0 3000 
4 2008 132228 174 31 60 7904.0 2715 
5 2009 140610 174 31 65 6761.0 2218 
6 2011 158559 174 31 68 10124.2 4637 
7 2012 944907 174 31 70  NA  NA 

在这里,使用cbind识别您希望通过聚合列。然后,您可以指定所有其他列,或者只需使用.来指示“使用cbind调用中未提及的所有其他列。”

+0

太好了,非常感谢! – Rafael

相关问题