# [R] how to replace values in x by means in subgroups created in ... (not loops)

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Jul 27 11:36:00 CEST 2011

```Something like this?

dataset <- data.frame(x = x, y = y)
dataset\$Group <- cumsum(c(0, diff(!is.na(dataset\$y) & dataset\$y == 0)) == 1)
library(plyr)
tmp <- ddply(subset(dataset, y == 0), .(Group), function(z){c(Mean = mean(z\$x, na.rm = TRUE))})
result <- merge(dataset, tmp)
result\$Mean[is.na(result\$y) | result\$y != 0] <- result\$x[is.na(result\$y) | result\$y != 0]

Best regards,

Thierry

> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
> Namens Eugeniusz Kaluza
> Verzonden: woensdag 27 juli 2011 11:21
> Aan: r-help op r-project.org
> Onderwerp: [R] how to replace values in x by means in subgroups created in ...
> (not loops)
>
>
> # 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 op r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help