输入:
library(dplyr)
A <- data_frame(Type=c("A","A","B"),R=c(3, 7, 12), Value = c(120, 130,300))
A
Type R Value
(chr) (dbl) (dbl)
1 A 3 120
2 A 7 130
3 B 12 300
映射数据集:
Mapping <- data_frame(Type=c("A","A","A","B","B","B"), Rmin = c(1,6,11,1,6,11), Rmax = c(5,10,15,5,10,15), Threshold = c(50,100,200,50,200,300))
Mapping
Type Rmin Rmax Threshold
(chr) (dbl) (dbl) (dbl)
1 A 1 5 50
2 A 6 10 100
3 A 11 15 200
4 B 1 5 50
5 B 6 10 200
6 B 11 15 300
预计将通过以下步骤获得输出。
isAbove <- 120>=50
因此,预期输出为:
B <- data_frame(Type=c("A","A","B"),R=c(3, 7, 12), Value = c(60, 90,250), isAbove = c(T,F,F))
B
Type R Value isAbove
(chr) (dbl) (dbl) (lgl)
1 A 3 120 TRUE
2 A 7 130 TRUE
3 B 12 300 FALSE
答案 0 :(得分:2)
使用data.table
的非等连接功能非常适合。使用:
library(data.table)
setDT(A)
setDT(Mapping)
A[Mapping, on = .(Type, R > Rmin, R < Rmax), isAbove := Value > Threshold]
给你:
> A Type R Value isAbove 1: A 3 120 TRUE 2: A 7 130 TRUE 3: B 12 300 FALSE