有没有办法使大规模匹配值更具程序性?基本上我想要做的是将一堆用于值查找的列添加到数据框中,但我不想每次都编写 match[] 参数。这似乎是mapply的一个用例,但我不太清楚如何在这里使用它。有什么建议么?
这是数据:
data <- data.frame(
region = sample(c("northeast","midwest","west"), 50, replace = T),
climate = sample(c("dry","cold","arid"), 50, replace = T),
industry = sample(c("tech","energy","manuf"), 50, replace = T))
以及相应的查找表:
lookups <- data.frame(
orig_val = c("northeast","midwest","west","dry","cold","arid","tech","energy","manuf"),
look_val = c("dir1","dir2","dir3","temp1","temp2","temp3","job1","job2","job3")
)
所以现在我要做的是:首先在“数据”中添加一个名为“reg_lookups”的列,它将将该区域与“查找”中的适当值相匹配。对“climate_lookups”等做同样的事情。
现在,我遇到了这个烂摊子:
data$reg_lookup <- lookups$look_val[match(data$region, lookups$orig_val)]
data$clim_lookup <- lookups$look_val[match(data$climate, lookups$orig_val)]
data$indus_lookup <- lookups$look_val[match(data$industry, lookups$orig_val)]
我尝试使用函数来执行此操作,但该函数似乎不起作用,因此将其应用于mapply是不行的(另外我对 mapply 语法如何在这里工作感到困惑):
match_fun <- function(df, newval, df_look, lookup_val, var, ref_val) {
df$newval <- df_look$lookup_val[match(df$var, df_look$ref_val)]
return(df)
}
data2 <- match_fun(data, reg_2, lookups, look_val, region, orig_val)