Да, как только что сказал Эндрю, возможно, лучше всего сначала создать справочную таблицу (= LUT) => создать фрейм данных с двумя столбцами (zip_code и value), например, так:
# Sample data set (because we don't have yours):
data = rbind.data.frame(cbind("dummy"="AA", "Zipcode"="41042"),
cbind("dummy"="BB", "Zipcode"="25911"),
cbind("dummy"="CC", "Zipcode"="41041"),
cbind("dummy"="DD", "Zipcode"="41038"),
cbind("dummy"="EE", "Zipcode"="05077"),
stringsAsFactors=F)
data_copy = data
# Build the LUT dataframe (here, only 2 vars with 3 records, but you can put 9 vars with 37 records if you need):
LUT = rbind.data.frame(cbind("zip_code"="25911", "White"=0.857, "Yellow"=0.112),
cbind("zip_code"="41041", "White"=0.952, "Yellow"=0),
cbind("zip_code"="41042", "White"=0.917, "Yellow"=NA), stringsAsFactors=F)
# Then, I load the dplyr library to get available the left_join function:
library(dplyr)
# Then, to get all the columns (here, White and Yellow) in the data object:
data <- left_join(data, LUT, by=c("Zipcode"="zip_code"))
# Or, instead of the previous line code, to get for example only the White column in the data object:
data_copy <- left_join(data_copy, LUT[, c("zip_code", "White")], by=c("Zipcode"="zip_code"))
# If you want to replace all the NA by 0 in a specific column (here, White), do this :
data_copy$White <- ifelse(is.na(data_copy$White), 0, data_copy$White)
Примечание: вы можете использовать функцию слияния вместо left_join, если хотите.