I have two data frames - one contains a bunch of postcodes and surveys allocated to them (df1
), the other contains all possible postcodes and the zones within which these are contained (df2
). I essentially need to remove the postcodes, and allocate each survey to a zone (using the postcodes to cross reference). Currently I estimate my program will take 5hrs. how can I speed this up?
for (i in 1:nrows(df1)) {
index <- which(df2$postcodes == toString(df1$postcodes[i])
if (length(index)) {
df1$zone <- toString(df2[index])
} else {
df1$zone <- 'UNMATCHED'
}
}
Currently, I've found that running this for 100 postcodes takes about 6 seconds.
I have tried a bunch of things for the past couple of hours with little to no progress, so any help would be much appreciated!