0

I have a dataframe of observations (Coral) that are geo referenced by latitude and longitude - lat and lon

I have another dataframe (Lat_Lon) which gives a sequence number (seq) to each grid point - lat, lon

How do I match the lat and lon from the Coral dataframe with the lat and lon from the Lat_Lon dataframe and return the sequence number (seq) to the Coral dataframe as a new column?

I want the dataframe of observations (Coral) to be geo referenced by the sequence number (seq) as well as by lat and lon

> summary.default(Coral)
      Length Class  Mode   
UNIQUE_ID 14705  -none- numeric
SURVEY_ID 14705  -none- numeric
lat       14705  -none- numeric
lon       14705  -none- numeric

> summary.default(Lat_Lon)
    Length Class  Mode   
seq 259200 -none- numeric
lon 259200 -none- numeric
lat 259200 -none- numeric

dput(head(Coral))
structure(list(UNIQUE_ID = 229:234, SURVEY_ID = c(2387L, 2387L, 
2387L, 2390L, 2390L, 2390L), lat = c(7.25, 7.25, 7.25, 7.25, 
7.25, 7.25), lon = c(99.25, 99.25, 99.25, 99.25, 99.25, 99.25
)), .Names = c("UNIQUE_ID", "SURVEY_ID", "lat", "lon"), row.names = c(NA, 
6L), class = "data.frame")

dput(head(Lat_Lon))
structure(list(seq = c(1, 2, 3, 4, 5, 6), lon = c(-179.75, -179.25, 
-178.75, -178.25, -177.75, -177.25), lat = c(89.75, 89.75, 89.75, 
89.75, 89.75, 89.75)), .Names = c("seq", "lon", "lat"), row.names = c(NA, 
6L), class = "data.frame")
tyzissou
  • 3
  • 6
  • You'd do better to output the results of `dput(head(Lat_Lon))` and `dput(head(Coral))` to your question so others can use your data. Also, take a look at `?merge`. – SymbolixAU Dec 12 '16 at 20:23
  • is `Lat_Lon` only one column, called `x..lon..lat` ? – SymbolixAU Dec 12 '16 at 20:40
  • No, it should be three separate columns, it's saved as a .txt file, so maybe an issue with R not recognizing the column breaks. I'll see if I can convert it to a .csv file. – tyzissou Dec 12 '16 at 20:42
  • it's more likely an issue with your command to read in the data. How is the .txt file formatted? comma separated, tab separated, ... ? – SymbolixAU Dec 12 '16 at 20:44
  • It's comma separated – tyzissou Dec 12 '16 at 20:46
  • I'm reading it in with: Lat_Lon <-read.csv('Lat_Lon.txt', header=TRUE) – tyzissou Dec 12 '16 at 20:47
  • Once you get the data read in correctly, the command you're after is `merge(x = Coral, y = Lat_Lon, by = c("lat","lon"))` – SymbolixAU Dec 12 '16 at 20:49
  • That seemed to work well, many thanks. There were some observations in the 'Coral' dataframe without 'lat' and 'lon' coordinates (with 'NA' instead), is there anyway to include these observations in the merged dataset? – tyzissou Dec 12 '16 at 21:11
  • take a look at `?merge`. You'll want to set `all.y = TRUE` – SymbolixAU Dec 12 '16 at 21:12
  • Great, many thanks, `all.x = TRUE` is was what I was after – tyzissou Dec 12 '16 at 21:18

0 Answers0