Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- judgeDF <- data.table(x_lb = c(1, 4, 7), x_ub = c(3, 6, 9),
- y_cate = paste0("M", 1:3), output = paste0("K", 1:3))
- judgeDF
- # x_lb x_ub y_cate output
- # 1: 1 3 M1 K1
- # 2: 4 6 M2 K2
- # 3: 7 9 M3 K3
- input <- unique(data.table(X = sample(1:10, 100, TRUE),
- Y = sample(paste0("M", 1:3), 100, TRUE)))
- head(input)
- # X Y
- # 1: 3 M2
- # 2: 10 M3
- # 3: 4 M3
- # 4: 3 M1
- # 5: 6 M3
- # 6: 6 M2
- judgeDF[input, .(X, Y, output), on = .(x_lb <= X, x_ub >= X, y_cate == Y)]
- # X Y output
- # 1: 3 M2 NA
- # 2: 10 M3 NA
- # 3: 4 M3 NA
- # 4: 3 M1 K1
- # 5: 6 M3 NA
- # 6: 6 M2 K2
- judgeDF <- data.frame(x_lb = c(1, 4, 7), x_ub = c(3, 6, 9),
- y_cate = paste0("M", 1:3), output = paste0("K", 1:3),
- stringsAsFactors = FALSE)
- input <- unique(data.frame(X = sample(1:10, 100, TRUE),
- Y = sample(paste0("M", 1:3), 100, TRUE),
- stringsAsFactors = FALSE))
- output <- rep(NA_character_, nrow(input))
- for (i in 1L:nrow(input)) {
- loc <- which(input[i, 1] >= judgeDF[, 1] & input[i, 1] <= judgeDF[, 2] &
- input[i, 2] == judgeDF[, 3])
- if (length(loc) > 0L) {
- output[i] <- judgeDF[loc, 4]
- } else {
- output[i] <- NA_character_
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment