Hi, You can also try this: dat1<-read.table(text=" Unique StepNo Data1 Data2 1 A 1 4 5 2 A 1 5 6 3 A 1 7 8 4 A 1 3 4 5 A 1 1 1 6 B 1 2 4 7 B 1 3 5 8 B 1 4 5 9 B 1 5 6 10 B 1 6 7 ",sep="",header=TRUE,stringsAsFactors=FALSE) dat2<-ddply(dat1,.(Unique,StepNo), head,2) dat3<-ddply(dat1,.(Unique,StepNo), tail,2) fun1 <- function(x,y,z,...){ xp <- do.call("paste", x) yp <- do.call("paste", y) zp <-do.call("paste",z) x[! ((xp %in% yp) | (xp %in% zp)), ] }
res<-fun1(dat1,dat2,dat3) res # Unique StepNo Data1 Data2 #3 A 1 7 8 #8 B 1 4 5 ddply(res,.(Unique,StepNo),numcolwise(mean)) #or dat2<-do.call(rbind,lapply(split(dat1,list(dat1$Unique,dat1$StepNo)),function(x) do.call(rbind,list(head(x,2),tail(x,2))))) fun2 <- function(x,y,...){ xp <- do.call("paste", x) yp <- do.call("paste", y) x[! xp %in% yp, ] } res2<-fun2(dat1,dat2) res2 # Unique StepNo Data1 Data2 #3 A 1 7 8 #8 B 1 4 5 ddply(res2,.(Unique,StepNo),numcolwise(mean)) A.K. ----- Original Message ----- From: siddu479 <onlyfordigitalst...@gmail.com> To: r-help@r-project.org Cc: Sent: Sunday, November 4, 2012 10:36 PM Subject: Re: [R] Excluding fixed number of rows from calculation while summarizing using ddply() function. Hi Arun, Thanks for your reply but your script is removing only one row( first row and last row) for each Unique and StepNo combination and calculating mean for the rest of rows. For below data , your script removing the #'s rows perfectly. But in reality I may need to ignore *say first 10 rows and last 20 rows for each Unique and StepNo combination. * for statistics calculation. Unique StepNo Data1 Data2 1 A 1 4 5 #Your script removing this row successfully. 2 A 1 5 6 3 A 1 7 8 4 A 1 3 4 5 A 1 1 1 #Your script removing this row successfully. 6 B 1 2 4 #Your script removing this row successfully. 7 B 1 3 5 8 B 1 4 5 9 B 1 5 6 10 B 1 6 7 #Your script removing this row successfully. Can you modify your script to get my requirement like below (making it generic, here *N=2*, removing first 2 lines and last 2 lines.. *sometimes I may have two numbers N1 & N2 (no.of rows need to be removed from and top and bottom respectively*) Unique StepNo Data1 Data2 1 A 1 4 5 #Ignore this 2 A 1 5 6 #Ignore this 3 A 1 7 8 4 A 1 3 4 #Ignore this 5 A 1 1 1 #Ignore this 6 B 1 2 4 #Ignore this 7 B 1 3 5 #Ignore this 8 B 1 4 5 9 B 1 5 6 #Ignore this 10 B 1 6 7 #Ignore this and then calculate the statistics using ddply. I hope my problem statement is much clear now. ----- Sidda Business Analyst Lead Applied Materials Inc. -- View this message in context: http://r.789695.n4.nabble.com/Excluding-fixed-number-of-rows-from-calculation-while-summarizing-using-ddply-function-tp4648406p4648447.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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. ______________________________________________ R-help@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.