0

I am currently facing the following problem with some housing data. I hava a dataframe which has more than 100,000 observations. For each observation there is a zip code observed. Let's assume the dataframe looks the following

size <- c(20, 30, 40, 50, 30, 45, 60)
rent <- c(100, 200, 300, 400, 350, 460, 700)
ZIP <- c(1000, 1500, 2000, 2500, 1500, 2000, 2500)
df <- data.frame(size, rent, ZIP)

I want to replace the individual zip code levels by other values (build categories on my own). These other values/ categories are stored in anouther data frame that assignes the new value to each zip code level. Say the data frame looks the following:

ZIP_levels <- c(levels(ZIP))
ZIP_New <- c(1, 1, 2, 2)
df2 <- data.frame(ZIP_levels, ZIP_New)

How can I create something like a VLOOKUP that can easily replace the ZIP-values in df with the ZIP_New values out of df2?

FJO
  • 1
  • 2

1 Answers1

0

A solution with dplyr

library(dplyr)

df_new <- left_join(df, df2, by = "ZIP")
bf_new$ZIP <- NULL
Conor Neilson
  • 1,026
  • 1
  • 11
  • 27