Introduction to R and Rstudio

R is a tool for working with data. It helps you organize data, make calculations, create graphs, and run statistical analyses. You can think of R as a very powerful calculator that also knows how to make plots and analyze real-world data (like precipitation, streamflow, or water quality).

R is not a point-and-click program like Excel. Instead, you give R instructions by typing commands. This may sound intimidating at first, but the upside is that your work is reproducible: you (or someone else) can rerun the same commands and get the same results every time.

RStudio is the program that makes working with R much easier. R and Rstudio can be downloaded for free on your personal computers. The software should be installed on your TNR 322 lab computers. If not you can install the software by going to the university’s “Software Center”, search for the software, and install it.

##Install Packages

In class we will go over navigation and the view. Installing packages in R adds extra tools and features that are not included by default. Packages are collections of functions created by other users to help with specific tasks, such as making plots, working with dates, or analyzing hydrologic data.

When you install a package, R downloads it to your computer so you can use it in your work. You only need to install a package once, but you need to load it each time you start a new R session. We will begin by installing packages using the code below:

install.packages(c(“dplyr”, “ggplot2”, “lubridate”))

C command

You may have noticed the “c” command nested in the library function. The c() command stands for combine. It groups multiple items into a single list so they can be used together. Note that when I use the C command, the elements in the list must be in quotes. This cannot be used with all functions. For example, it cannot be used with the library command below. It can be used with install.packages command.

Library Command

The library() command loads an installed package into your current R session so you can use its functions. Installing a package makes it available on your computer, but library() is what turns it on for the current session.

You need to run library() each time you start RStudio if you want to use that package. The command line is writtend as follows:

library(dplyr)

Setting the Working Directory and importing files

The working directory is the folder where R looks for data files and saves results. If your data are in this folder, you can refer to files by name instead of typing long file paths. An easier and recommended option is to use an RStudio Project, which automatically sets the working directory and keeps all files organized in one place. Keep data files inside your project folder for easy access. It is best to learn how to write the command line for setting the working directory. An example of how to set the working directory is the line of code below:

setwd(“C:/Users/kclancy/research/Discharge”)

The function or command is “setwd” and the folder address is in quotes. Also note that all the slashes are forward slashes (right-leaning). You can copy file directories in windows, but you will need to change the directions of the slashes.

Assigning a variable

You use <- (most common) or = to store a value in a variable.

x <- 5 y = 10

Both of these assign values, but <- is the preferred and conventional R style because it clearly signals assignment and avoids confusion in more complex code.

Using the equal sign (==)

You use == to compare two values, not assign them.

x == 5

This asks: “Is x equal to 5?” The result is a logical value: TRUE or FALSE.

Why this matters

x <- 5 → assigns 5 to x

x == 5 → tests whether x equals 5

Writing x = 5 can work for assignment, but it can be confusing inside functions where = is also used to match arguments.

Quick summary

<- → assign a value (recommended)

= → can assign, but mostly used for function arguments

== → compare values

In this class we will assign values using the assignment operator.

Assignment Operator Exercise

Run the following code in Rstudio:

x<-5
y<-3

x==3
## [1] FALSE
y==5
## [1] FALSE

Make sure your output matches mine. Note the differences in the way the syntax handles the assignment.