我希望这个问题还没有得到回答,并希望我能找到正确的词汇(因为我不知道如何用几句话来表达我的问题)。
基本上,我有一列所谓的SIC码的数据。我还有一个查找表,它将每个SIC-代码分类为一个特定的类别。现在,我想要匹配的SIC-代码与类别编号。但是,查找表只给出了SIC代码的范围,即行包含:
如果我有类似于1111的SIC代码,但是循环表列2是1000,第3列是1500,那么简单的匹配函数就不能工作。我创造了一个更好理解的例子:
test <- as.data.frame(c(1012, 2010, 3545, 5550, 7068))
colnames(test) <- "SIC"
ind_num <- c(1, 3, 4, 5, 7, 10, 11, 12, 14, 15)
sic_low <- c(0, 1010, 1012, 1050, 2000, 2005, 3500, 5550, 7050, 8000)
sic_high <- c(20, 1011, 1020, 1099, 2002, 2020, 3545, 5551, 7070, 8010)
LUPtable <- data.frame(ind_num, sic_low, sic_high)
test$new <- lapply(test$SIC, function(x) LUPtable$ind_num[match(x, LUPtable$sic_low)])提前感谢!
发布于 2018-06-26 17:06:35
或类似的
test$new <- lapply(test$SIC, function(x) LUPtable$ind_num[x>=LUPtable$sic_low & x<=LUPtable$sic_high])发布于 2018-06-26 16:43:48
您可以对每个值进行迭代,使用vapply()进行测试,并获取其低值和高值之间的索引。
LUPtable$ind_num[vapply(test$SIC, function(x) which(x >= LUPtable$sic_low & x <= LUPtable$sic_high), numeric(1))]https://stackoverflow.com/questions/51047555
复制相似问题