From your code, it appears that zipmsa is a SAS format defined using PROC FORMAT.
I suggest two possible solutions. Either create a data frame comprising the zip and censusmsa pairs which can be used as a lookup table, or store this mapping as a named character vector (the name is the zip code and the value is the censusmsa).
The example code below demonstrates both the possibilities.
# Dataset containing zip and corresponding name
zip_map_d <- data.frame(
zip = c(1, 2, 3),
name = c("A", "B", "C"),
stringsAsFactors = FALSE
)
# Character vector where the name of each element is the zip code
zip_map_v <- c("1" = "A", "2" = "B", "3" = "C")
# Dataset containing zip codes and some random value
df <- data.frame(
zip = sample(c(1, 2, 3), 10, replace = TRUE),
x = rnorm(10)
)
# Left join using the zip code
library(dplyr)
df2 <- df %>%
left_join(zip_map_d, by = c("zip" = "zip"))
# Create a new variable by looking up the named character vector
df3 <- df %>%
mutate(name = zip_map_v[zip])