Guest User

Untitled

a guest
Jan 31st, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. library(data.table)
  2. >
  3. > dat <- data.table(x = sample(c(10, 16, 22, 28, 34, 40), 20, replace = TRUE),
  4. + y = sample(seq(0.5, 19.5, by = 1), 20, replace = TRUE))
  5. >
  6. >
  7. > TempData <- dat[, .(Counting = .N), .(x, y)][order(x, y)]
  8. > TempData <- TempData[, Counting := cumsum(Counting), x]
  9. >
  10. > TempData <- dat[, .(Number = .N), x][TempData, on = "x"]
  11. >
  12. > TempData <- TempData[, result := Counting / Number]
  13. > TempData[, `:=`(Number = NULL, Counting = NULL)]
  14. >
  15. > A<-as.matrix(TempData)
  16. > A
  17. x y result
  18. [1,] 10 8.5 0.5000000
  19. [2,] 10 12.5 1.0000000
  20. [3,] 16 0.5 0.2500000
  21. [4,] 16 5.5 0.5000000
  22. [5,] 16 12.5 0.7500000
  23. [6,] 16 17.5 1.0000000
  24. [7,] 22 5.5 0.5000000
  25. [8,] 22 7.5 1.0000000
  26. [9,] 28 0.5 0.2000000
  27. [10,] 28 2.5 0.4000000
  28. [11,] 28 8.5 0.6000000
  29. [12,] 28 18.5 0.8000000
  30. [13,] 28 19.5 1.0000000
  31. [14,] 34 2.5 0.2500000
  32. [15,] 34 10.5 0.5000000
  33. [16,] 34 11.5 0.7500000
  34. [17,] 34 13.5 1.0000000
  35. [18,] 40 0.5 0.3333333
  36. [19,] 40 3.5 0.6666667
  37. [20,] 40 8.5 1.0000000
  38. >
Advertisement
Add Comment
Please, Sign In to add comment