Guest User

Untitled

a guest
Jan 30th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. library(data.table)
  2.  
  3. # Generating given dataset
  4. dat <- data.table(x = sample(c(10, 16, 22, 28, 34, 40), 2000, replace = TRUE),
  5. y = sample(seq(0.5, 19.5, by = 1), 2000, replace = TRUE))
  6.  
  7. # Counting how many individuals in each x and add them up
  8. TempData <- dat[, .(Counting = .N), .(x, y)][order(x, y)]
  9. TempData <- TempData[, Counting := cumsum(Counting), x]
  10.  
  11. # Combine original dataset which in order to acquire the number of
  12. # counting of individuals
  13. TempData <- dat[, .(Number = .N), x][TempData, on = "x"]
  14.  
  15. # Calculating final required metric
  16. TempData <- TempData[, result := Counting / Number]
  17. TempData[, `:=`(Number = NULL, Counting = NULL)]
Advertisement
Add Comment
Please, Sign In to add comment