'Naming after zip codes (not US!)

In advance, I hope I've presented a data example appropriately - I haven't posted here before and I'm new to R as well. I have a LARGE dataset where I would like to create a new column with regional name (5 different) based on a lot of zip codes (1089 different) as shown below.

id zip 
1 8000   
2 7700 
3 1050
4 5000  
5 6880 
6 8620 


id zip  region_name
1 8000   central
2 7700   north
3 1050   capital  
4 5000   south
5 6880   central 
6 8620   central

Due to the many different zip codes, I have assigned each of the 1089 zip codes into five lists() according to the five regions: "north", "central", "south", "capital", and "sjaelland". Can anyone help me with a good code/solution?

Thanks!



Solution 1:[1]

I suggest to use stack to make your zip code named list a data.frame as well and then merge/left_join the tables

code

zip_code_df <- stack(zip_code_list)
names(zip_code_df) <- c("zip", "region_name")

left_join(df, zip_code_df, by = "zip")

results

   id  zip region_name
1:  1 8000     central
2:  2 7700       north
3:  3 1050     capital
4:  4 5000       south
5:  5 6880     central
6:  6 8620     central

data

df <- structure(list(id = 1:6, zip = c(8000L, 7700L, 1050L, 5000L, 6880L, 8620L)), row.names = c(NA, -6L), class = c("data.frame"))

zip_code_list <- list(
  "north" = c(7700),
  "central" = c(8000, 6880, 8620),
  "capital" = c(1050),
  "south" = c(5000)
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Merijn van Tilborg