Skip to content
Back to formatted view

Raw Message

Message-ID: <1393631459.80953.YahooMailNeo@web142606.mail.bf1.yahoo.com>
Date: 2014-02-28T23:50:59Z
From: arun
Subject: aggregate and sum on some columns fromduplicate rows
In-Reply-To: <1393628516546-4686043.post@n4.nabble.com>

Hi,
You could use ?ddply
library(plyr)

cbind(ddply(dat,.(St.Sam,Sp),function(x) x[!duplicated(x$Var1),])[,-c(5:6)],ddply(dat,.(St.Sam,Sp),colwise(sum,.(NT,PT)))[,-c(1:2)])
?? St.Sam? Sp Var1 Var2 NT PT
1? ST1.S1 Sp1?? 12?? aa 23 37
2? ST1.S1 Sp2?? 32?? bb 45 26
3? ST1.S1 Sp3?? 47?? cc 89 35
4? ST1.S2 Sp1?? 25?? dd 29 66
5? ST1.S2 Sp2?? 59?? ee 89 35
6? ST2.S1 Sp1?? 15?? aa 30 45
7? ST2.S1 Sp2?? 45?? cc 55 23
8? ST2.S1 Sp3?? 27?? aa 85 12
9? ST2.S1 Sp4?? 42?? cc? 8? 3
10 ST3.S1 Sp1?? 25?? aa 26 69
11 ST3.S1 Sp2?? 36?? bb 65 48
A.K.




On Friday, February 28, 2014 6:47 PM, ltdm <Luis.Tito-de-Morais at ird.fr> wrote:
Hi again,

Sorry for disturbing. After posting I suddenly found a solution.
As it may help someone else here it goes.

df
du1 <- duplicated(df[,c("St.Sam","Species")],fromLast = F)
du2 <- duplicated(df[,c("St.Sam","Species")],fromLast = T)
X <- df[du1|du2,]
aggRows <- aggregate(cbind(NT,PT) ~St.Sam+Species+Var1+Var2, X,sum)
dfNew <- rbind(df[!(du1|du2),], aggRows)

Just need to "polish" dfNew and its OK. But maybe there is a more elegant
solution.

Cheers,

Tito





--
View this message in context: http://r.789695.n4.nabble.com/aggregate-and-sum-on-some-columns-fromduplicate-rows-tp4686040p4686043.html

Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.