Computing Agroclimate Metrics:

Intro to R


Andy Lyons
March 26, 2026

https://ucanr-igis.github.io/agroclimR_ucm/

R Projects

Precision Irrigation Calculator
https://ucanr-igis.shinyapps.io/irrigation-calc/

Pistachio Nut Growth Calculator
https://ucanr-igis.shinyapps.io/pist_gdd/

Tree Chill Calculator for Cherry
https://ucanr-igis.shinyapps.io/cherrychill/

Navel Orangeworm IPM Economics Calculator
https://ucanr-igis.shinyapps.io/now_ipm_econ/

Chill Portions Under Climate Change Calculator
https://ucanr-igis.shinyapps.io/chill/

Goals

Working with R

  • package
  • vector
  • function
  • argument
  • piping
  • tidyverse
  • data frame
  • tibble
  • data wrangling
  • ggplot

Agroclimate Metrics


R and RStudio

Why is R So Popular?

  1. It’s free!

  2. Huge user community (especially academics)

  3. Thousands of add-ons (packages) that extend its capabilities

  4. Particularly strong in plotting and reporting

  5. Once you get over the initial hump, can work very efficiently

  6. Makes it easy to get your code “out there”

  7. Solid overall programming language



Exercise 1: RStudio Exploration and Basic Commands

Exercise 1 Topics

  1. Using R like a fancy calculator
  2. Order of operations
  3. Comparison operators
  4. Saving the results of expressions to variable
  5. Rules for naming variables

Posit Cloud project for this workshop:

https://bit.ly/agroclim-ucm

After it opens:


Exercise Review

Key vocabulary terms are in italic.

Agroclimate Metrics

Agroclimate metrics reflect weather driven factors that influence the development of plants and insects.

Examples:

These are abiotic factors but they influence biotic factors that affect crops (like disease).

The main ingredients of metrics are measurements of weather variables (like air temperature).

We can compute them for the past, present, and future.

Why are they useful?

Weather derived metrics are generally good predictors of growth


Pistachio nut embryo length vs degree days
Zhang et al (2021)


Managing Farm Operations

Optimize Pest Management

  • Timing is everything in integrated pest management
  • Save $$ and reduce harmful side effects


Scheduling

  • Irrigation scheduling and amounts
  • When to start deficit irrigation
  • Put out traps
  • Schedule harvest crews
  • Plan for the coming season


Understanding the Effects of Climate Change


https://doi.org/10.1016/j.ufug.2018.07.020
Where can I go today to see the climate my city will have 50 years from now?
How many generations of Navel Orangeworm will growers have to deal with in the coming decades?
https://doi.org/10.1016/j.scitotenv.2020.142657

https://doi.org/doi:10.1371/journal.pone.0020155
How much chill can we expect in the coming decades? Will there be enough to have a economically viable farming operation?
What kind of frost exposure will we see mid-century?
https://doi.org/10.1016/j.scitotenv.2020.143971

https://doi.org/10.3390/agronomy12010205
What is the ‘new normal’ for agroclimatic metrics for specialty crops?

Why would you want to do these in R?

R is an extremely flexible computing environment,
with strengths in:

  • data manipulation
  • visualization
  • data modeling
  • integrating other data
  • widely taught
  • strong user community


Access weather and climate data



owmr: OpenWeatherMap API Wrapper
rnoaa: ‘NOAA’ Weather Data from R
riem: Accesses Weather Data from the Iowa Environment Mesonet
…plus many others

Or write your own!


Weather Data

Station Based

  • Highest quality data are from sensors
    mounted on weather stations
  • Many public and private networks
  • Many are online
  • Closer is better
  • On-site is best
  • Near ground is good

Typical variables:
  • air temperature
  • solar radiation
  • relative humidity
  • dew point
  • precipitation
  • soil temperature
  • soil moisture
  • vapor pressure
  • wind speed
  • wind direction

Interpolated / Gridded Data

  • based on weather station data with modeling
  • PRISM (continental USA, 1979 - present, 800m & 4km)
  • gridMET (continental USA, based on PRISM)
  • Livneh (continental USA, 1950–2013, 6km)

Accessing Data

File server

  • files may cover a large area
  • files may be big
  • you have to read the file and
    extract the AOI yourself

API service

  • communication between computers
  • just get what you need
  • may require registration
  • may require a subscription fee

R Packages

Packages are what R calls extensions or add-ons.

What’s in a package?

Functions

The keys to R’s superpowers are functions! There are four things you need to know to use a function:


Which function should I use?

Finding the right R function, half the battle is.

- Jedi MasteR Yoda

Ask your friends

Ask Google

Cheatsheets!


Piping

Piping syntax is an alternative way of writing arguments into functions.

With piping, you use the pipe operator |> (or %>%) to ‘feed’ the result of one function into the next function.

Piping allows the results of one function to be passed as the first argument of the next function. Hence a series of commands to be written like a sentence.

Example

Consider the expression:

zoo(moo(boo(foo(99)),n=4))


Data Frames & Tibbles

R has two data classes that organize data in rows and columns:


Key concepts


Data Wrangling: What do we mean?

Whatever is needed to get your data frame ready
for the function(s) you want to use for analysis and visualization.

also called data munging, manipulation, transformation, etc.

Often includes one or more of:

  1. dropping columns
  2. renaming columns
  3. changing the order of columns
  4. creating new columns with an expression
  5. filtering rows
  6. sorting rows
  7. going from ‘long’ to ‘wide’ formats
  8. joining data frames based on a common field
  9. merging data frames together
  10. splitting tables
  11. aggregating rows into groups


Data wrangling is a pain!

But R (specifically the tidyverse packages) makes it (a lot) easier.


Data Wrangling with dplyr

An alternative (usually better) way to wrangle data frames than base R.

Part of the tidyverse.

Best way to familiarize yourself - explore the cheat sheet:


Forecasting Almond Bloom

Theoretical example:

Step 1: Predict when dormancy breaks


Step 2: Predict when bloom occurs after dormancy


Exercise 2: Import Weather Data & Compute Chill Portions

Exercise 2 Topics

  1. Run code in a script
  2. Import saved weather data (CIMIS Station 206)
  3. Plot it
  4. Compute chill portions

https://bit.ly/agroclim-ucm

Exercise 2 Review


Scripts

Top five advantages of using scripts over the console:

  1. Easier to write (and fix!) your code
  2. You can add comments to remind yourself what each command is doing
  3. Reuse your own code
  4. You can add loops and if-then statements later on
  5. Tell your friends you’re a coder!


Exercise 3: Compute Chill Portions for the Coming Decades

END!