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,47,57))
We could use
rleid
to create another grouping variable, get themean
of 'y', and assign the 'indx' to NULLBenchmarks
You could identify groups of consecutive elements and then identify the mean and value for each: