Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r search along a vector and calculate the mean

Tags:

r

vector

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,47,57))
like image 385
Øystein Myrland Avatar asked Jun 06 '15 17:06

Øystein Myrland


3 Answers

We could use rleid to create another grouping variable, get the mean of 'y', and assign the '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 
like image 129
akrun Avatar answered Sep 30 '22 15:09

akrun


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
like image 37
bgoldst Avatar answered Sep 30 '22 15:09

bgoldst


You could identify groups of consecutive elements and then identify the mean 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
like image 27
josliber Avatar answered Sep 30 '22 15:09

josliber