r search along a vector and calculate the mean
问题 I have data that looks like: require(data.table) DT <- data.table(x=c(19,19,19,21,21,19,19,22,22,22), y=c(53,54,55,32,44,45,49,56,57,58)) I would like to search along x, and calculate the means for y. However, when using. DT[, .(my=mean(y)), by=.(x)] I get the overall means for the coinciding values of x. I would like to search along x, and each time x changes, I would like to calculate a new mean. For the provided example, the output would be: DTans <- data.table(x=c(19,21,19,22), my=c(54,38