# Dear all, # how to replace values in x by means in subgroups created in ... # replace only these values where y=0 in continous sequence # replace by mean calculated locally for each subgroup created by # continous sequence of 0,0,0 in parallel y vector, i.e. # where there is continous sequence of 0 in data frame vector y # but we do not replace values in x[i], if y[i]!=0 # we do not want use loops we do not use apply (not very fast)
x <-c(0 ,1,2,3,4,5,6,7,8,NA,NA,1 ,1 ,NA,2 ,2) y <-c(0 ,0,0,1,0,0,0,1,1,1 ,NA,0 ,0 ,0 ,0 ,1) Must_be<-c(1 ,1,1,3,5,5,5,7,8,NA,NA,1.5,1 ,NA,1.5,2) (df<-as.data.frame(cbind(x,y)) ) # I have traied many bad colusions based on cumsum, pmin, pmax, ... (mean_dfx_if_yIs0<-y*cumsum(x*y)/(cumsum(y)*y) ) # how to do this? # thans for any advice # E [[alternative HTML version deleted]] ______________________________________________ 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.