Re:_: how to replace values in x by means in subgroups created in ...(not loops)
Thanks, below some code and reply:
#_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_#
#---
# my slow function with loops:
# replace_x_by_locallyMean_x_4_0s_in_y.f(x,y)
#-
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(resul
2 matches
Mail list logo