Computing Agroclimate Metrics:

Intro to R


Andy Lyons
March 24, 2025

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

About Me…

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.

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!