HI GG, I should had checked with multiple t=0 only rows. Apologies! Check if this work: (Changed the thread name as the solution applies to that problem)
dat2<- read.csv("dat6.csv",header=TRUE,sep="\t",row.names=1) str(dat2) #'data.frame': 3896 obs. of 3 variables: # $ patient_id: int 2 2 2 2 2 2 2 2 2 2 ... # $ t : int 0 1 2 3 4 5 6 7 8 9 ... # $ basdai : num 2.83 4.05 3.12 3.12 2.42 ... library(plyr) dat2New<-ddply(dat2,.(patient_id),summarize,t=seq(min(t),max(t))) res<-join(dat2New,dat2,type="full") lst1<-lapply(split(res,res$patient_id),function(x) {x1<-x[x$t!=0,];do.call(rbind,lapply(split(x1,((x1$t-1)%/%3)+1),function(y) {y1<-if(any(y$t==1)) rbind(x[x$t==0,],y) else y; data.frame(patient_id=unique(y1$patient_id),t=head(y1$t,1),basdai=mean(y1$basdai,na.rm=TRUE))}) ) }) dat3<-dat2[unlist(with(dat2,tapply(t,patient_id,FUN=function(x) x==0 & length(x)==1)),use.names=FALSE),] head(dat3,3) # patient_id t basdai #143 10 0 5.225 #555 37 0 2.450 #627 42 0 6.950 lst2<-split(dat3,seq_len(nrow(dat3))) lst1[lapply(lst1,length)==0]<-mapply(rbind,lst1[lapply(lst1,length)==0],lst2,SIMPLIFY=FALSE) res1<-do.call(rbind,lst1) row.names(res1)<- 1:nrow(res1) res2<- res1[,-2] res2$period<-with(res2,ave(patient_id,patient_id,FUN=seq_along)) #res2 #selected rows res2[c(48:51,189:192,210:215),] # patient_id basdai period #48 9 3.625000 8 #49 10 5.225000 1 #t=0 only row #50 11 6.018750 1 #51 11 6.000000 2 #189 36 6.166667 1 #190 37 2.450000 1 #t=0 only row #191 38 3.100000 1 #192 38 3.575000 2 #210 41 1.918750 1 #211 41 4.025000 2 #212 41 2.975000 3 #213 41 1.725000 4 #214 42 6.950000 1 #t=0 only row #215 44 4.300000 1 A.K. ________________________________ From: GUANGUAN LUO <guanguan...@gmail.com> To: arun <smartpink...@yahoo.com> Sent: Thursday, May 23, 2013 9:50 AM Subject: Re: how to calculate the mean in a period of time? Hello, Arun, sorry to trouble you again, I tried your method and i found that for patient_id==10 et patient_id==37 ect, the scores are repeated 51 times, I don't understand why this occured. Thank you so much. GG ______________________________________________ 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.