0

I want to

  • Read GEOID from df1
  • Match it with fips in df2.
  • Read countyflashiness corresponding to matching fips in df2, add it to df1 in a new column. I don't the other columns of DF2.

Tried this, but not working:

merge(df1, df2, by.x='GEOID10', by.y='fips')

df1 (dput 4 levels)

structure(list(X = 1:4, GEOID10 = c(45001L, 22001L, 51001L, 16001L
), STATE_FIPS = c(45L, 22L, 51L, 16L), STATE = structure(c(3L, 
2L, 4L, 1L), .Label = c("IDAHO", "LOUISIANA", "SOUTH CAROLINA", 
"VIRGINIA"), class = "factor"), COUNTY = structure(1:4, .Label = c("ABBEVILLE", 
"ACADIA", "ACCOMACK", "ADA"), class = "factor")), .Names = c("X", 
"GEOID10", "STATE_FIPS", "STATE", "COUNTY"), row.names = c(NA, 
4L), class = "data.frame")

df2

structure(list(row = c(48.16, 48.33, 48.15, 48.1), col = c(-124.7, 
-124.69, -124.69, -124.69), m = c(0.4713, 0.8998, 0.4891, 0.8418
), flagar = c(0L, 1L, 0L, 1L), flagk = c(0L, 0L, 0L, 0L), flagsi = c(0L, 
0L, 0L, 0L), flags2o = c(0L, 0L, 0L, 0L), flagap = c(0L, 0L, 
0L, 0L), flagmt = c(0L, 0L, 0L, 0L), flagcn = c(0L, 0L, 0L, 0L
), flagkf = c(1L, 1L, 1L, 1L), flagrd = c(1L, 1L, 1L, 1L), flagrv = c(2L, 
2L, 2L, 2L), flagpt = c(1L, 1L, 1L, 1L), coords.x1 = c(-124.7, 
-124.69, -124.69, -124.69), coords.x2 = c(48.16, 48.33, 48.15, 
48.1), fips = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_
), countyflashiness = c(0.4713, 0.8998, 0.4891, 0.8418)), .Names = c("row", 
"col", "m", "flagar", "flagk", "flagsi", "flags2o", "flagap", 
"flagmt", "flagcn", "flagkf", "flagrd", "flagrv", "flagpt", "coords.x1", 
"coords.x2", "fips", "countyflashiness"), row.names = c(NA, 4L
), class = "data.frame")
maximusdooku
  • 5,242
  • 10
  • 54
  • 94

0 Answers0