R: fast sliding window with given coordinates

前端 未结 2 662
耶瑟儿~
耶瑟儿~ 2021-02-08 19:09

I have a data table with nrow being around a million or two and ncol of about 200.

Each entry in a row has a coordinate associated with it.

Tiny portion of the d

2条回答
  •  梦谈多话
    2021-02-08 19:52

    Rollapply works great with a small dataset. However, if you are working with several million rows (genomics) it is quite slow.

    The following function is super fast:

    data <- c(runif(100000, min=0, max=.1),runif(100000, min=.05, max=.1),runif(10000, min=.05, max=1), runif(100000, min=0, max=.2))
    slideFunct <- function(data, window, step){
      total <- length(data)
      spots <- seq(from=1, to=(total-window), by=step)
      result <- vector(length = length(spots))
      for(i in 1:length(spots)){
        result[i] <- mean(data[spots[i]:(spots[i]+window)])
      }
      return(result)
    }
    

    Details here.

提交回复
热议问题