我编写了一个简单的函数来映射表中的变量并返回相应列中的值。这个想法与Excel中的vlookup非常相似。我写的函数如下:
ApplyMap <- function(mappingtable,variable){
if(ncol(mappingtable)!=2){
"Mapping table needs to have two columns"
}else{
names(mappingtable) <- c("col1","col2")
output <- mappingtable[col1==variable]$col2[1]
output <- as.character(if(is.na(output)){variable}else{output})
return(output)
}
}
我的地图:
require(data.table)
mapping <- structure(list(Field1 = structure(c(4L, 1L, 2L, 3L), .Label = c("Amsterdam",
"Arnhem", "Groningen", "Rotterdam"), class = "factor"), Field2 = structure(c(4L,
3L, 1L, 2L), .Label = c("Gelderland", "Groningen", "Noord-Holland",
"Zuid-Holland"), class = "factor")), .Names = c("Field1", "Field2"
), row.names = c(NA, -4L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7f93f1018578>)
当我将它应用于一个简单的变量时,例如:
ApplyMap(mapping,"Arnhem")
[1] "Gelderland"
或
ApplyMap(mapping,"New York")
[1] "New York"
它看起来工作正常。但是,现在我想将它应用于以下data.table并在适当的地图中添加另一列和第二列。列表如下:
list <- structure(list(List = structure(c(6L, 1L, 2L, 3L, 4L, 5L, 7L), .Label = c("Amsterdam",
"Arnhem", "Groningen", "Haarlem", "Maastricht", "Rotterdam",
"Utrecht"), class = "factor")), .Names = "List", class = "data.frame", row.names = c(NA,
-7L))
我尝试使用以下代码执行此操作:
list$Province <- ApplyMap(mapping,list$List)
不幸的是,这似乎不合适。有没有人可以帮助我理解为什么它不能正常工作?当我执行上面的行时,我收到以下错误消息:
Error in Ops.factor(col1, variable) : level sets of factors are different
很明显,我想从映射表的第二列中获取表的第二列。