R quick start guide

This article introduces some basic R concepts that may help when using the UKFE package as well as links to other R resources.

R basics

Numerical operations

In R, we can perform arithmetic operations, for example, try typing the following in the console:

1 + 2
#> [1] 3
3 - 5
#> [1] -2
4 * 2
#> [1] 8
6 / 3
#> [1] 2

Objects

An object acts as a container for storing data. For example, you can assign some text (called a string) to the object called greeting:

greeting <- "Hello!"

You can then use this variable later:

greeting
#> [1] "Hello!"

Numbers can be stored in numeric variables:

number <- 1

These can then be used in arithmetic operations:

number + 10
#> [1] 11

Vectors

Vectors are one-dimensional arrays that store data. Each element has the same data type. To create one, you can use the c function, which stands for combine or concatenate. UKFE uses lots of numeric vectors.

vec <- c(1, 2, 3, 4)

You can perform calculations with vectors:

a <- c(1, 2, 3)
b <- c(4, 5, 6)
a + b
#> [1] 5 7 9
a <- c(1, 2, 3)
a * 2
#> [1] 2 4 6

Data frames

Data frames are structures made up of rows and columns, similar to the structure of a csv file. Each column in a data frame is a vector.

column_1 <- c(1, 2, 3, 4)
column_2 <- c(4, 3, 2, 1)

df <- data.frame(column_1, column_2)
head(df)
#>   column_1 column_2
#> 1        1        4
#> 2        2        3
#> 3        3        2
#> 4        4        1

Functions

R contains some built-in functions. For example, we can use the mean() function to find the mean of a vector:

mean(c(1, 2, 3))
#> [1] 2

We can also use a variable inside a function:

vector_of_numbers <- c(2, 4, 6)
mean(vector_of_numbers)
#> [1] 4

You can learn about the functions in the UKFE package using this documentation and the functions’ help files.

Learning resources for R

If you are finding the UKFE documentation difficult to follow, or if you would like to further develop your knowledge of R, there are many resources available to help. The following suggestions are particularly useful for those who are new to R or programming in general.

Online books

Interactive courses

Webinars and video tutorials

Blogs and tutorials

Support and questions