# Addition
3 + 7[1] 10
Here are some R basics that will help you get started with Study Questions and Assignments.
Much of this is adpated from http://www.sthda.com/english/wiki/easy-r-programming-basics
The basic arithmetic operators are:
+ (addition)- (subtraction)* (multiplication)/ (division)^ (exponentiation).Type directly the command below in the console:
A variable can be used to store a value.
For example, the R code below will store the day of year (DOY) in a variable as “DOY”:
Note that, it’s possible to use <- or = for variable assignments (athough as noted above, <- is more common)
To print the value of the created object, just type its name:
or use the function print() (you’ll see later use you use the function cat() to display output):
You can also perform basic arithmetic using your variable
You can change the value of the object:
You can also write equations using previously defined variables. The following R code creates two variables including the width and the height of a rectangle. These two variables will be used to compute the area of the rectangle.
Basic data types are numeric, character and logical.
It’s possible to use the function class() to see what type a variable is:
You can also use the functions is.numeric(), is.character(), is.logical() to check whether a variable is numeric, character or logical, respectively. For instance:
If you want to change the type of a variable to another one, use the as.* functions, including: as.numeric(), as.character(), as.logical(), etc.
A vector is a combination of multiple values (numeric, character or logical) in the same object. In this case, you can have numeric vectors, character vectors or logical vectors.
A vector is created using the function c() (for concatenate), as follow:
[1] 1 81 172 365
Note: a vector can only hold elements of the same type. For example, you cannot have a vector that contains both characters and numeric values.
Find the length of a vector (i.e., the number of elements in a vector)
Case of missing values
In R missing values (or missing information) are represented by NA:
[1] 1 81 NA 365
It’s possible to use the function is.na() to check whether a data contains missing value. The result of the function is.na() is a logical vector in which, the value TRUE specifies that the corresponding element in x is NA.
Subsetting a vector consists of selecting a part of your vector.
Selection by positive indexing: select an element of a vector by its position (index) in square brackets
Selection by negative indexing: Exclude an element
For more on subsetting vectors, see here
Note that, all the basic arithmetic operators (+, -, *, / and ^ ) as well as the common arithmetic functions (log, exp, sin, cos, tan, sqrt, abs, …), described in the previous sections, can be applied on a numeric vector.
If you perform an operation with vectors, the operation will be applied to each element of the vector. An example is provided below:
[1] -22.9817392 -0.1007028 23.3991332 -23.0541505
Other useful functions are:
max(x) # Get the maximum value of x
min(x) # Get the minimum value of x
# Get the range of x. Returns a vector containing
# the minimum and the maximum of x
range(x)
length(x) # Get the number of elements in x
sum(x) # Get the total of the elements in x
prod(x) # Get the product of the elements in x
# The mean value of the elements in x
# sum(x)/length(x)
mean(x)
sd(x) # Standard deviation of x
var(x) # Variance of x
# Sort the element of x in ascending order
sort(x)When you load a dataset, it will generally be loaded as a data frame (e.g., the datasets you will be loading for your Assignments). However, here are some simple examples to help you get more familiar with data frames.
A data frame can be created using the function data.frame(), as follow:
DOY albedo Qh Qe
1 1 0.40 10 20
2 81 0.10 400 100
3 172 0.34 350 500
4 365 0.90 -40 12
To check whether a data is a data frame, use the is.data.frame() function. Returns TRUE if the data is a data frame:
To select just certain columns from a data frame, you can either refer to the columns by name or by their location (i.e., column 1, 2, 3, etc.).
Positive indexing by name and by location
For more on subsetting a data frame see here. On this page you can also find more opperations you can perform using data frames.
While there are multiple ways to create a new variable in a data frame, as simple approach is to add a new variable directly to the data frame by creating a new column and assigning values to it.For example, add a new variable to calculate the Bowen ratio using Qh and Qe (recall the bowen ratio = Qh/Qe).
DOY albedo Qh Qe bowen_ratio
1 1 0.40 10 20 0.500000
2 81 0.10 400 100 4.000000
3 172 0.34 350 500 0.700000
4 365 0.90 -40 12 -3.333333
Note that while you will not need to install packages when using RStudio Server (since they are pre-installed), this is helpful if you need to install packages using R installed on your own computer.
To install an R package, we can use the install.packages() function:
or you can install multiple packages using
Now when you need to use a package in an Assignment or Study Question, you can call it using
Here we will use the packe ggplot to create some basic plots.
Use the R code below:
The data set mtcars is used in the examples below:
mpg cyl wt
Mazda RX4 21.0 6 2.620
Mazda RX4 Wag 21.0 6 2.875
Datsun 710 22.8 4 2.320
Hornet 4 Drive 21.4 6 3.215
Hornet Sportabout 18.7 8 3.440
Valiant 18.1 6 3.460
ggplot(): build plots piece by piece
This section describes briefly how to use the function ggplot(). Recall that, the concept of ggplot divides a plot into three different fundamental parts: plot = data + Aesthetics + geometry.
To demonstrate how the function ggplot() works, we’ll draw a scatter plot. The function aes() is used to specify aesthetics.


Question: How would you convert this to a line graph? Try asking chatgpt (or equivalent) ;)
(RStudio Server or Your Own Computer — Mac & PC)
These instructions apply whether you are working on RStudio Server or using R and RStudio installed on your own computer.
The key idea is the same in both cases: keep your files organized and use simple file paths.
Before writing any code, make sure that: - Your assignment file (.Rmd) - Your data files (.xlsx, .csv) - Any images used in the document
are all saved in the same folder.
This prevents file-path errors and ensures your work runs correctly on different computers and on the RStudio Server.
Packages only need to be installed once per computer or server account.
You should not reinstall packages every time you render your document.
Every time you start a new session, you must load the packages you are using.
Place this code chunk near the top of your .Rmd file.
If your Excel file is in the same folder as your .Rmd, use only the file name.
If your CSV file is in the same folder as your .Rmd, again use only the file name.
If you see an error like “file not found” or “cannot open the connection”, check the following:
.Rmd?Following the steps above will prevent most file-loading problems in this course.
Note: this goes beyond the basics and provides more in depth information on handling of dates in R for those that are curious/interested.
R has multiple packages to handle dates, including base R, lubridate, and chron. The most commonly used is lubridate (part of the tidyverse).
Install the package if needed:
Common date formats