Updates records in a location table. Records are identified by locationID and the data found in locationData is used to replace any existing value in the columnName column. locationID and locationData must be of the same length. Any NA values in locationID will be ignored.

If columnName is not a named column within locationTbl, a new column will be created.

table_updateColumn(
  locationTbl = NULL,
  columnName = NULL,
  locationID = NULL,
  locationData = NULL,
  verbose = TRUE
)

Arguments

locationTbl

Tibble of known locations.

columnName

Name of an existing/new column in locationTbl whose data will be updated/created.

locationID

Vector of locationID strings.

locationData

Vector of data to be inserted at records identified by locationID.

verbose

Logical controlling the generation of progress messages.

Value

Updated tibble of known locations.

Examples

library(MazamaLocationUtils)

locationTbl <- get(data("wa_monitors_500"))
wa <- get(data("wa_airfire_meta"))

# We will merge some metadata from wa into locationTbl

# Record indices for wa
wa_indices <- seq(5,65,5)
wa_sub <- wa[wa_indices,]

locationID <- 
  table_getLocationID(
    locationTbl, 
    wa_sub$longitude, 
    wa_sub$latitude, 
    distanceThreshold = 500
  )
  
locationData <- wa_sub$AQSID

locationTbl <- 
  table_updateColumn(locationTbl, "AQSID", locationID, locationData)

# Look at the data we attempted to merge
wa$AQSID[wa_indices]
#>  [1] "530710006"    "840MM0510008" "840530250003" "530450007"    "530470010"   
#>  [6] "530330030"    "840MM0370180" "530150015"    "530330023"    "530070011"   
#> [11] "530570015"    "840MM0399990" "530750005"   

# And two columns from the updated locationTbl
locationTbl_indices <- table_getRecordIndex(locationTbl, locationID)
locationTbl[locationTbl_indices, c("city", "AQSID")]
#> # A tibble: 13 × 2
#>    city         AQSID       
#>    <chr>        <chr>       
#>  1 Burbank      530710006   
#>  2 Newport      840MM0510008
#>  3 Soap Lake    840530250003
#>  4 Shelton      530450007   
#>  5 Winthrop     530470010   
#>  6 Seattle      530330030   
#>  7 Cle Elum     840MM0370180
#>  8 Longview     530150015   
#>  9 Enumclaw     530330023   
#> 10 Wenatchee    530070011   
#> 11 Mount Vernon 530570015   
#> 12 White Salmon 840MM0399990
#> 13 LaCrosse     530750005