Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rm(list=ls(all=TRUE))
- library(pipeR) # %>>%
- library(data.table) # fread
- library(Matrix) # sparseMatrix
- top_p <- 10
- dt <- 600
- colNames <- c("group", "second", "id")
- txtFiles <- list.files(".", pattern="\\.txt")
- inputDT <- fread(txtFiles[1]) %>>% setnames(colNames) %>>%
- `[`(, orderSecond := frankv(second, ties.method = "first"), by = group)
- st = proc.time()
- maxLen <- inputDT[ , list(len = length(second)), by = group] %>>% (max(.$len))
- outDT <- lapply(1:(maxLen-1), function(j){
- inputDT[ , dataID := id[j], by = group] %>>%
- `[`( , secondDiff := second - second[j], by = group) %>>%
- `[`(orderSecond - j > 0) %>>%
- `[`(!is.na(secondDiff) & secondDiff < dt)
- }) %>>% rbindlist
- m11 <- outDT %>>% `[`( , .(dataID, id))
- m21 <- outDT %>>% `[`(secondDiff == 0 , .(dataID, id))
- time_method1 <- proc.time() - st
- st = proc.time()
- u = unique(inputDT$group)
- m1 = array(dim=c(0,4))
- m2 = array(dim=c(0,4))
- for (i in 1:length(u)){
- y = inputDT[inputDT$group==u[i],]
- for(j in 1:(nrow(y)-1)){
- y$d = c(rep(NA,j), tail(y$second,-j)-y$second[j])
- k1 = which(y$d < dt)
- k2 = which(y$d == 0)
- if(length(k1)>0) {
- m1 = rbind(m1, cbind(u[i], j, id=y$id[j], y$id[k1]))
- }
- if(length(k2)>0) {
- m2 = rbind(m2, cbind(u[i], j, y$id[k2], pid=y$id[j]))
- }
- }
- }
- time_method2 <- proc.time() - st
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement