在R中使用具有连续值的查找表

时间:2016-04-01 02:43:44

标签: r join match lookup

我在R中有一个查找表,我试图弄清楚如何实现。我面临的挑战是它涉及连续的数值或数据范围。如果值介于两者之间我希望选择正确的值。

我想使用两个连续的'GRADE','SAT'变量加上分类'TYPE'值来分配'GROUP'值。这一大块代码看起来令人生畏,但这些都是微不足道的小桌子。

任何建议都值得赞赏!!!!

    #lookup table code for recreating dataframe
     structure(list(Type = structure(c(1L, 2L, 1L, 1L), .Label = c("A", 
"B"), class = "factor"), min_grade = c(93L, 85L, 93L, 80L), max_grade = c(100L, 
93L, 100L, 92L), min_sat = c(600L, 700L, 400L, 600L), max_sat = c(800L, 
800L, 599L, 800L), Group = structure(c(1L, 1L, 2L, 3L), .Label = c("A", 
"B", "C"), class = "factor")), .Names = c("Type", "min_grade", 
"max_grade", "min_sat", "max_sat", "Group"), class = "data.frame", row.names = c(NA, 
-4L))


#example ----- desired value is in the 'GROUP' column so this would be NULL before I used the lookup table


           structure(list(Name = structure(c(3L, 1L, 2L, 4L), .Label = c("Jack", 
    "James", "John", "Jordan"), class = "factor"), Grade = c(95L, 
 95L, 92L, 93L), Sat = c(701L, 500L, 800L, 800L), Type = structure(c(1L, 
1L, 1L, 2L), .Label = c("A", "B"), class = "factor"), Group = structure(c(1L, 
2L, 3L, 1L), .Label = c("A", "B", "C"), class = "factor")), .Names = c("Name", 
"Grade", "Sat", "Type", "Group"), class = "data.frame", row.names = c(NA, 
-4L))

2 个答案:

答案 0 :(得分:1)

这是怎么回事?

ltab <- structure(list(Type = structure(c(1L, 2L, 1L, 1L), .Label = c("A", 
    "B"), class = "factor"), min_grade = c(93L, 85L, 93L, 80L), max_grade = c(100L, 
    93L, 100L, 92L), min_sat = c(600L, 700L, 400L, 600L), max_sat = c(800L, 
    800L, 599L, 800L), Group = structure(c(1L, 1L, 2L, 3L), .Label = c("A", 
    "B", "C"), class = "factor")), .Names = c("Type", "min_grade", 
    "max_grade", "min_sat", "max_sat", "Group"), class = "data.frame", row.names = c(NA, 
    -4L))


dat <- structure(list(Name = structure(c(3L, 1L, 2L, 4L), .Label = c("Jack", 
    "James", "John", "Jordan"), class = "factor"), Grade = c(95L, 
    95L, 92L, 93L), Sat = c(701L, 500L, 800L, 800L), Type = structure(c(1L, 
    1L, 1L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("Name", 
    "Grade", "Sat", "Type"), class = "data.frame", row.names = c(NA, 
    -4L))

library(plyr)
mdat <- adply(merge(dat, ltab, by="Type", all=T), 1, function(x) {
     c(FallsIn=x$Grade > x$min_grade & x$Grade <= x$max_grade & x$Sat > x$min_sat & x$Sat <= x$max_sat)
})
mdat[mdat$FallsIn,]

考虑概括,是否会有更多连续变量需要检查?

编辑:无法编辑OP帖子,因此考虑OP的评论是我如何处理&#34;对多维连续随机变量进行分类的一个例子&#34; 这样,这些关键字将在未来的搜索中标记

breaks <- list(Var1=c(0, 0.25, 1),
    Var2=c(0, 0.5, 1),
    Var3=c(0, 0.25, 0.75, 1))

#generate this on the fly
genIntv <- function(x) {
    ret <- paste0("(", x[1:(length(x)-1)],", ",x[2:length(x)], "]")
    names(ret) <- 1:(length(x)-1)
    ret
}
lookupTbl <- data.frame(expand.grid(lapply(breaks, genIntv), stringsAsFactors=F), 
    Group=LETTERS[1:12])
lookupTbl2 <- data.frame(expand.grid(lapply(breaks, function(x) 1:(length(x)-1)), stringsAsFactors=F), 
    Group=LETTERS[1:12])

#data set
dat <- data.frame(Var1=c(0.1, 0.76), Var2=c(0.5, 0.75), Var3=c(0.25,0.9))
binDat <- do.call(cbind, setNames(lapply(1:ncol(dat), function(k) 
    .bincode(dat[,k], breaks[[k]], T, T)),colnames(dat)))
merge(binDat, lookupTbl2, all.x=T, all.y=F)

如果其他人有更好的方法,那就太好了解

答案 1 :(得分:0)

如果你的数据很小,那么完整的连接应该没问题。

library(dplyr)
result = 
  example %>%
  select(-Type) %>%
  full_join(look_up) %>%
  filter(min_grade < Grade & Grade <= max_grade &
           min_sat < Sat & Sat <= max_sat)