Re: [R] aggregate and sum on some columns fromduplicate rows

2014-02-28 Thread arun
If, 'dat' is the dataset: res <- as.data.frame(do.call(rbind,lapply(split(dat,list(dat$St.Sam,dat$Sp),drop=TRUE),function(x) c(unlist(x[1,1:4]),colSums(x[,5:6])  row.names(res) <- 1:nrow(res) res    St.Sam  Sp Var1 Var2 NT PT 1  ST1.S1 Sp1   12   aa 23 37 2  ST1.S2 Sp1   25   dd 29 66 3  ST2.

Re: [R] aggregate and sum on some columns fromduplicate rows

2014-02-28 Thread arun
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  ST

Re: [R] aggregate and sum on some columns fromduplicate rows

2014-02-28 Thread ltdm
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

[R] aggregate and sum on some columns fromduplicate rows

2014-02-28 Thread ltdm
Hi list, I have a dataframe df looking like this one: St.SamSpVar1 Var2NTPT ST1.S1Sp112aa2032 ST1.S1Sp232bb4526 ST1.S1Sp112aa 3 5 ST1.S1Sp347cc8935 ST1.S2Sp125dd2966 ST1.S2Sp2