我想在两个数据框之间匹配州和区号,从而获取第一个数据框的农作物的产量和产量。
有人可以帮助我以更有效的方式替换下面的嵌套循环:
for (i in 1:length(og_county2$State)){
for (j in 1:length(km2$State)){
if (is.na(og_county2$`Value.CORN, GRAIN - ACRES HARVESTED`[i]) == "TRUE" & (as.character(og_county2$State[i]) == as.character(km2$State[j])) & (as.character(og_county2$District.code[i]) == as.character(km2$Ag.District.Code[j]))& km2$County[j] =="OTHER (COMBINED) COUNTIES"){
og_county2$`Value.CORN, GRAIN - ACRES HARVESTED`[i] = km2$`Value.CORN, GRAIN - ACRES HARVESTED`[j]
}
if (is.na(og_county2$`Value.CORN, GRAIN - PRODUCTION, MEASURED IN BU`[i]) == "TRUE" & (as.character(og_county2$State[i]) == as.character(km2$State[j])) & (as.character(og_county2$District.code[i]) == as.character(km2$Ag.District.Code[j]))& km2$County[j] =="OTHER (COMBINED) COUNTIES"){
og_county2$`Value.CORN, GRAIN - PRODUCTION, MEASURED IN BU`[i] = km2$`Value.CORN, GRAIN - PRODUCTION, MEASURED IN BU`[j]
}
if (is.na(og_county2$`Value.CORN, GRAIN - YIELD, MEASURED IN BU / ACRE`[i]) == "TRUE" & (as.character(og_county2$State[i]) == as.character(km2$State[j])) & (as.character(og_county2$District.code[i]) == as.character(km2$Ag.District.Code[j]))& km2$County[j] =="OTHER (COMBINED) COUNTIES"){
og_county2$`Value.CORN, GRAIN - YIELD, MEASURED IN BU / ACRE`[i] = km2$`Value.CORN, GRAIN - YIELD, MEASURED IN BU / ACRE`[j]
}
}
}