R search by vector and calculate the average value

I have data that looks like this:

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 by x and calculate the means for y. However, when used.

DT[, .(my=mean(y)), by=.(x)]

I get generic tools for matching x values. I would like to search by x, and every time x changes, I would like to calculate a new value. For the given example, the output will look as follows:

DTans <- data.table(x=c(19,21,19,22),
             my=c(54,38,47,57))
+4
source share
3 answers

We could use rleidgrouping to create another variable, get meanof 'y' and set 'indx' to NULL

library(data.table) # v 1.9.5+
DT[, .(my = mean(y)), by = .(indx = rleid(x), x)][, indx := NULL]
#    x my
#1: 19 54
#2: 21 38
#3: 19 47
#4: 22 57

Benchmarks

set.seed(24)
foo <- function(x) sample(x, 1e7L, replace = TRUE)
DT  <- data.table(x = foo(100L), y = foo(10000L))

josilber <- function() {
    new.group <- c(1, diff(DT$x) != 0)
    res <- data.table(x = DT$x[new.group == 1], 
              my = tapply(DT$y, cumsum(new.group), mean))
}

Roland <- function() {
    DT[, .(my = mean(y), x = x[1]), by = cumsum(c(1, diff(x) != 0))]
}

akrun <- function() { 
    DT[, .(my = mean(y)), by = .(indx = rleid(x), x)][,indx := NULL]
}

bgoldst <- function() {
    with(rle(DT$x), data.frame(x = values, 
       my = tapply(DT$y, rep(1:length(lengths), lengths), mean)))
}

system.time(josilber())
#   user  system elapsed 
#159.405   1.759 161.110 

system.time(bgoldst())
#   user  system elapsed 
#162.628   0.782 163.380 

system.time(Roland())
#   user  system elapsed 
# 18.633   0.052  18.678 

system.time(akrun())
#   user  system elapsed 
# 1.242   0.003   1.246 
+10
source

You can identify groups of consecutive elements, and then determine the average value and value for each:

(new.group <- c(1, diff(DT$x) != 0))
# [1] 1 0 0 1 0 1 0 1 0 0
DT[, list(x = x[1L], my = mean(y)), by = list(indx = cumsum(new.group))]
#    indx  x my
# 1:    1 19 54
# 2:    2 21 38
# 3:    3 19 47
# 4:    4 22 57
+3
with(rle(DT$x),data.frame(x=values,my=tapply(DT$y,rep(1:length(lengths),lengths),mean)));
##    x my
## 1 19 54
## 2 21 38
## 3 19 47
## 4 22 57
+3

All Articles