Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- # Generating given dataset
- dat <- data.table(x = sample(c(10, 16, 22, 28, 34, 40), 2000, replace = TRUE),
- y = sample(seq(0.5, 19.5, by = 1), 2000, replace = TRUE))
- # Counting how many individuals in each x and add them up
- TempData <- dat[, .(Counting = .N), .(x, y)][order(x, y)]
- TempData <- TempData[, Counting := cumsum(Counting), x]
- # Combine original dataset which in order to acquire the number of
- # counting of individuals
- TempData <- dat[, .(Number = .N), x][TempData, on = "x"]
- # Calculating final required metric
- TempData <- TempData[, result := Counting / Number]
- TempData[, `:=`(Number = NULL, Counting = NULL)]
Advertisement
Add Comment
Please, Sign In to add comment