Это можно решить с помощью dplyr и tidyr.Я уверен, что есть и другие решения.
# create the data
df1 <- data.frame(pcodes = c(1001, 1002, 1003))
df2 <- data.frame(regions = c(1, 2),
pcodes = c("1001, 1002, 1003", "1004, 1005"),
stringsAsFactors = FALSE)
library(dplyr)
library(tidyr)
# separate postcodes column and reshape long
# (from https://stackoverflow.com/a/33288868/2633645)
df2 <- df2 %>%
mutate(to = strsplit(pcodes, split = ",")) %>%
unnest(to) %>%
mutate(to = as.numeric(to)) %>%
select(-pcodes) %>%
rename(pcodes = to) # rename `to` to `pcodes` for join purpose
# join the data sets by the common variable pcodes
df_both <- left_join(df1, df2)
df_both
pcodes regions
1 1001 1
2 1002 1
3 1003 1