When data and the weather measurements do not perfectly line up, perform a linear interpolation between two weather measurements and merge the results into the provided dataset. Only applies to numerical weather columns (see weather for more details).

weather_interp(
  data,
  weather,
  cols = "all",
  interval = "hour",
  na_gap = 2,
  quiet = FALSE
)

Arguments

data

Dataframe. Data with dates or times to which weather data should be added.

weather

Dataframe. Weather data downloaded with weather which should be interpolated and added to data.

cols

Character. Vector containing the weather columns to add or 'all' for all relevant columns. Note that some measure are omitted because they cannot be linearly interpolated (e.g., wind direction).

interval

What interval is the weather data recorded at? "hour" or "day".

na_gap

How many hours or days (depending on the interval) is it acceptable to skip over when interpolating over NAs (see details).

quiet

Logical. Suppress all messages (including messages regarding missing data, etc.)

Details

Dealing with NA values If there are NAs in the weather data, na_gap can be used to specify a tolerance. For example, a tolerance of 2 with an interval of "hour", means that a two hour gap in data can be interpolated over (i.e. if you have data for 9AM and 11AM, but not 10AM, the data between 9AM and 11AM will be interpolated. If, however, you have 9AM and 12PM, but not 10AM or 11AM, no interpolation will happen and data between 9AM and 12PM will be returned as NA.)

Examples

# Weather data only head(kamloops)
#> # A tibble: 6 x 35 #> station_name station_id station_operator prov lat lon elev climate_id #> <chr> <dbl> <lgl> <chr> <dbl> <dbl> <dbl> <chr> #> 1 KAMLOOPS A 51423 NA BC 50.7 -120. 345. 1163781 #> 2 KAMLOOPS A 51423 NA BC 50.7 -120. 345. 1163781 #> 3 KAMLOOPS A 51423 NA BC 50.7 -120. 345. 1163781 #> 4 KAMLOOPS A 51423 NA BC 50.7 -120. 345. 1163781 #> 5 KAMLOOPS A 51423 NA BC 50.7 -120. 345. 1163781 #> 6 KAMLOOPS A 51423 NA BC 50.7 -120. 345. 1163781 #> # … with 27 more variables: WMO_id <chr>, TC_id <chr>, date <date>, #> # time <dttm>, year <chr>, month <chr>, day <chr>, hour <chr>, weather <chr>, #> # hmdx <dbl>, hmdx_flag <chr>, pressure <dbl>, pressure_flag <chr>, #> # rel_hum <dbl>, rel_hum_flag <chr>, temp <dbl>, temp_dew <dbl>, #> # temp_dew_flag <chr>, temp_flag <chr>, visib <dbl>, visib_flag <chr>, #> # wind_chill <dbl>, wind_chill_flag <chr>, wind_dir <dbl>, #> # wind_dir_flag <chr>, wind_spd <dbl>, wind_spd_flag <chr>
# Data about finch observations at RFID feeders in Kamloops, BC head(finches)
#> # A tibble: 6 x 10 #> animal_id date time logger_id species age sex #> <fct> <date> <dttm> <fct> <chr> <chr> <chr> #> 1 041868FF… 2016-03-01 2016-03-01 06:57:42 2300 Mounta… AHY U #> 2 041868FF… 2016-03-01 2016-03-01 06:58:41 2300 Mounta… AHY U #> 3 041868FF… 2016-03-01 2016-03-01 07:07:21 2300 Mounta… AHY U #> 4 06200003… 2016-03-01 2016-03-01 07:32:34 2400 House … SY M #> 5 06200003… 2016-03-01 2016-03-01 07:32:35 2400 House … SY M #> 6 06200003… 2016-03-01 2016-03-01 07:32:36 2400 House … SY M #> # … with 3 more variables: site_name <chr>, lon <dbl>, lat <dbl>
# Match weather to finches # \donttest{ ## Not run finch_weather <- weather_interp(data = finches, weather = kamloops)
#> temp is missing 4 out of 4368 data, interpolation may be less accurate as a result.
#> temp_dew is missing 4 out of 4368 data, interpolation may be less accurate as a result.
#> rel_hum is missing 4 out of 4368 data, interpolation may be less accurate as a result.
#> wind_spd is missing 4 out of 4368 data, interpolation may be less accurate as a result.
#> visib is missing 4 out of 4368 data, interpolation may be less accurate as a result.
#> pressure is missing 4 out of 4368 data, interpolation may be less accurate as a result.
#> hmdx is missing 4163 out of 4368 data, interpolation may be less accurate as a result.
#> wind_chill is missing 3810 out of 4368 data, interpolation may be less accurate as a result.
# }