Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1.  
  2. rm(list=ls(all=TRUE))
  3. library(pipeR) # %>>%
  4. library(data.table) # fread
  5. library(Matrix) # sparseMatrix
  6.  
  7. top_p <- 10
  8. dt <- 600
  9. colNames <- c("group", "second", "id")
  10. txtFiles <- list.files(".", pattern="\\.txt")
  11. inputDT <- fread(txtFiles[1]) %>>% setnames(colNames) %>>%
  12. `[`(, orderSecond := frankv(second, ties.method = "first"), by = group)
  13.  
  14. st = proc.time()
  15. maxLen <- inputDT[ , list(len = length(second)), by = group] %>>% (max(.$len))
  16. outDT <- lapply(1:(maxLen-1), function(j){
  17. inputDT[ , dataID := id[j], by = group] %>>%
  18. `[`( , secondDiff := second - second[j], by = group) %>>%
  19. `[`(orderSecond - j > 0) %>>%
  20. `[`(!is.na(secondDiff) & secondDiff < dt)
  21. }) %>>% rbindlist
  22. m11 <- outDT %>>% `[`( , .(dataID, id))
  23. m21 <- outDT %>>% `[`(secondDiff == 0 , .(dataID, id))
  24. time_method1 <- proc.time() - st
  25.  
  26.  
  27. st = proc.time()
  28. u = unique(inputDT$group)
  29. m1 = array(dim=c(0,4))
  30. m2 = array(dim=c(0,4))
  31. for (i in 1:length(u)){
  32. y = inputDT[inputDT$group==u[i],]
  33. for(j in 1:(nrow(y)-1)){
  34. y$d = c(rep(NA,j), tail(y$second,-j)-y$second[j])
  35. k1 = which(y$d < dt)
  36. k2 = which(y$d == 0)
  37. if(length(k1)>0) {
  38. m1 = rbind(m1, cbind(u[i], j, id=y$id[j], y$id[k1]))
  39. }
  40. if(length(k2)>0) {
  41. m2 = rbind(m2, cbind(u[i], j, y$id[k2], pid=y$id[j]))
  42. }
  43. }
  44. }
  45. time_method2 <- proc.time() - st
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement