0

I have created another column, and in the new column - I would like the categories of high, low and medium based on zipcodes.

I have the list of zipcodes that are considered 'high', 'medium', and 'low' in a csv file.

I am able to do it individually, but with over 200 zipcodes, it becomes more difficult


This is what I've done for Individually:

#created new column
clinics$rate <- NA

#zipcode 97006 is considered low based on summary data
clinics$rate[clinics$zipcode==97006] <- 'low'

what can I do to tag it without entering it individually (over 200 zip codes)?

morgan121
  • 2,213
  • 1
  • 15
  • 33
Julia Liu
  • 1
  • 2
  • 1
    `clinics$rate[clinics$zipcode %in% lowzipcodes] <- 'low'` ? – Ronak Shah Jul 17 '20 at 02:37
  • Hi, Welcome to Stack Overflow. If you have two dataframes, one with zipcodes and your original info (say `df1`), and another with zipcodes and low, medium or high (say `df2`), you coudl join them like this; `library(dplyr)` then `final_df <- left_join(df1, df2)`, assuming zipcode had the same heading in both. To make the categories straight in `df1`, you could look at `case_when()` – Mark Neal Jul 17 '20 at 04:31
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 17 '20 at 04:58

1 Answers1

0

Say that your summary table of the zipcodes and ratings is called zips, then the following code should do the trick

clinics$rate <- zips$rate[matchclinics$zipcode, zips$zipcode]

What we're doing is using match() to index the zips$rate to output the ratings in the same order and number as the clinics dataframe

Jarn Schöber
  • 309
  • 1
  • 8