1

I have a dataset with numbers 1:70 which correspond to a neighborhood name within a city. The data is set up like so:

    Date | Area | Count
   04/16    1       12
   04/16    1       1
   05/16    2       3
   06/16    3       10

I have another dataframe with the area number and corresponding area name.

Number | Name
  1       Franklin
  2       State

How can I assign the area number value to its corresponding name without having to type out each number and name?

2 Answers2

2

You can do this with match

## Your data
dat = read.table(text="   Date  Area  Count
   04/16    1       12
   04/16    1       1
   05/16    2       3
   06/16    3       10",
header=TRUE, stringsAsFactors=FALSE)

areas = read.table(text="Number  Name
  1       Franklin
  2       State",
header=TRUE, stringsAsFactors=FALSE)

areas$Name[match(dat$Area, areas$Number)]
[1] "Franklin" "Franklin" "State"    NA  
G5W
  • 36,531
  • 10
  • 47
  • 80
2

It's easier if you give us reproducible data, but something like this:

df1$AreaName <- df2$Name[match(df1$Area, df2$Area)]
user3640617
  • 1,546
  • 13
  • 21