R Basics

Tim Newbold

2021-10-06

Overview

In this session, we will cover some of the basics of working with R:

  • Getting started with R
  • Conducting basic arithmetic
  • Variable assignment
  • Exploring variables
  • Manipulating variables
  • Code structuring and commenting

Getting Started with R

If you are starting with R for the first time, I recommend playing around with R code on this great website. This allows you to get to grips with the basics without having to install the R software (more on that in a later session).

Throughout all of the material in my introduction to R sessions, you may find this cheatsheet helpful (thanks to Mhairi McNeill for making this available).

Conducting Basic Arithmetic

If you type a single number into R, perhaps not surprisingly R simply returns that number (here the top line is what I have entered, and the bottom line is what R returns):

1.0
## [1] 1

Things get more useful if we ask R to perform some basic arithmetic:

1.0 + 2.0
## [1] 3
5.0/2
## [1] 2.5

Variable Assignment

So far, we have just used R as a glorified calculator. It gets more powerful when we save numbers as variables, allowing them to be re-used and manipulated. We assign variables like this:

x <- 1.0

Because R has simply assigned the variable x, the result isn’t yet displayed. But we can ask R to return the value of x:

x
## [1] 1

You can also reverse the direction of the arrows in variable assignment, although you will rarely see this approach used in practice:

1.0 -> x
x
## [1] 1

Variables can also be assigned to take the result of an arithmetic operation, for example:

y <- 1.0 + 2.0
y
## [1] 3

Exploring variables

Once you have assigned some variables in R, it is useful to be able to find out what variables you have, and to find out what those variables are. I will now introduce a couple of functions that are very useful for this.

First, to see what variables you have in your R environment, you can use the built-in ls function. Running ls shows us that we have created 2 variables, x and y:

ls()
## [1] "x" "y"

TIP: To find out what a function does, and how to use it, try the help function

help(ls)

If I specify a new variable, you will see that this now appears in our environment list:

a <- 3.0
ls()
## [1] "a" "x" "y"

To find out what type of information a variable contains, we can use the class function. I will explain more about data types in R later, but for now we can see that x is a number:

class(x)
## [1] "numeric"

The str function is also useful. This tells us what type of information we have (‘num’ is short for numeric here), and also the value of our variable. This function is more useful for more complex data types (of which more later):

str(x)
##  num 1

Manipulating Variables

We can manipulate variables we have created, for example performing arithmetic on them:

x * 3
## [1] 3

We can assign the results of these manipulations as new variables:

z <- x*3
z
## [1] 3

We can also overwrite our original variable (which can sometimes be useful, but take care that this doesn’t lead to mistakes):

x
## [1] 1
x<-x*3
x
## [1] 3

We can also remove variables from our R environment. We do this using the rm function. First, let’s remind ourselves which variables we currently have:

ls()
## [1] "a" "x" "y" "z"

Now, let’s remove the variable z:

rm(z)
ls()
## [1] "a" "x" "y"

Code Structuring and Commenting

To finish this session, a short note on code structuring and commenting.

You can write multiple operations on a single line of R code, if you separate them with a semi-colon:

x <- 1.0; y <- x * 3; y
## [1] 3

However, for clarity, it is generally better to have one operation per line, as we have done so far.

When you write code to run multiple operations in R, it is good to add comments explaining what each line of your code does. You can do this by starting your comment line with a hash tag:

# Assign the variable x
x <- 1.0

# Multiply x by 3, saving as a new variable y
y <- x * 3

# Display y
y
## [1] 3

Next Time

That’s it for this session. Next time, I will introduce some of the different basic data types in R.