Chapter 3 "Beginnings": The Interface
The R interface, regardless of which platform it is run on consists of two main parts. The first is the Console - where commands are input and run, and error messages displayed. This serves as the main working space for an R session. Secondly there is the main Menu Bar - here one can access standard File, Edit and Help commands.
Under the File Menu you can save and open a new script, or alternatively save your current R history - all of the commands and objects you hvae currently worked with, or load a saved file. Importantly you can also change your Working Directory via the File Menu, to navigate to where your data or scripts are stored. One can only directly load data from within the current working directory.
The Console - First Uses as A Calculator
Now lets tap into some of R's capabilities. Lets crunch some numbers…. :)
Anything we type into R is known as an expression. Each expression below is commented to provide additional detail or information about that line of code. It may explain the function, arguments or provide special notes to consider.
When working in any scientific endevaour it is good practice to consistently comment in your workflow. This not only helps remind youself what you were doing, but also acts is an valuable aid to any one whom may assess or run your code later.
25 * 2
## [1] 50
# create a sequence of numbers, assign to the variable 'x' x <- 1:10 # print the variable 'x' x
## [1] 1 2 3 4 5 6 7 8 9 10
# create a sequeunce of numbers x <- seq(1, 10, by = 2) # print the variable 'x' x
## [1] 1 3 5 7 9
# concatenate a vector i.e. create a sequence X <- c(1, 2, 3, 4, 5) # notice X and x are two different variables - naming is case sensitve! X
## [1] 1 2 3 4 5
R acts as both a simple calculator as shown above, and additionally a more advanced manipulator of so-called objects. Lets learn more about the different types of object you will encounter.
Objects: vectors, matrics, lists and data.frames
Believe it or not but in our first example we have already encountered our first objects!! Objects are something (and anything!) that can be assigned to a variable (in ordianry language we can think of this as “giving the object a name”). For example by using the <- we assigned a simple sequence of numbers (the object) to the variable called 'x'.
This programming technique allows us to recall and display the object at will - by simply typing in the variable name. In our first examples we used the letters 'x' and 'X' as our variable names.
This is the fundamental nature or R - it is an object-orientated language. This means that our data manipulation is based on the fact that data we work with is represented in the form of objects which can be assigned and saved as to a variable. Later we can access and manipulate these data in our workspace via their respective variable names: performing calculations, running functions and much more.
There are several main object types. Lets introduce each in turn:
Vectors
Vectors underpin the majority of calculations in R. They are the simplest type of object - vectors can be numeric, character, and logical [TRUE, FALSE].
# a numeric vector c(100, 200, 300, 400, 500)
## [1] 100 200 300 400 500
# a character vector c("Red", "Green", "Blue")
## [1] "Red" "Green" "Blue"
# a logical vector c(T, F, F, T)
## [1] TRUE FALSE FALSE TRUE
Constructing vectors
Here are a few more ways to construct vectors using the concatenate function. As shown below we can combine several arguments within the parenthesis to produce more eleborate sequences:
# combining the standard concatenate function with seq c(1:3, seq(15, 20, length = 10))
## [1] 1.00 2.00 3.00 15.00 15.56 16.11 16.67 17.22 17.78 18.33 18.89 ## [12] 19.44 20.00
# replicate the vector a given no. of times rep(1:3, 3)
## [1] 1 2 3 1 2 3 1 2 3
# replicate combined with length rep(1:5, length = 18)
## [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3
Manipulating Vectors
Vectors can be intuitively manipulated with common mathematical expressions:
# multiplied or divided c(1, 10, 2, 20) * 5
## [1] 5 50 10 100
# addition of the elements of two vectors c(1, 2, 3) + c(4, 2, 4)
## [1] 5 4 7
# test a expression i.e >, <, >=, <=, ==. yields a LOGICAL vector c(25, 100, 50, 200) > 75
## [1] FALSE TRUE FALSE TRUE
We usually want to work with multiple vectors at the same time. So as mentioned earlier its essential to assign a suitable variable name to each vector when we create it…
# assign a vector to the variable one.to.five one.to.five <- c(1, 2, 3, 4, 5) # assign a sequence of numbers to the variable my.vector my.vector <- c(1:20)
We can also apply some pre-designated functions to these vectors as shown:
# calculate the sum sum(one.to.five)
## [1] 15
# calculate the mean mean(one.to.five)
## [1] 3
# calculate the median median(one.to.five)
## [1] 3
# calculate the std dev sd(one.to.five)
## [1] 1.581
Object management
Numeric vectors can contain integers, decimals as well as not-a-number (NaN), which is often used to denote missing data.
# create an 'incomplete' vector some.data <- c(1, 2, NaN, 4, NaN) # print contents of variable some.data
## [1] 1 2 NaN 4 NaN
We can keep track of which objects are currently in our workspace by using the ls() command:
# show the 4 objects, each is of the type vector ls()
## [1] "my.vector" "one.to.five" "some.data" "x" "X"
We can check the length of our objects
# show length of vector length(some.data)
## [1] 5
Using the str() we can find out the properties of any object
# a numeric vector str(one.to.five)
## num [1:5] 1 2 3 4 5
With some practice creating and manipulating vectors will become second-nature.
Matrices and Arrays
Matrics can be seen as vectors with additional dimensions.
Lets create our first matrix:
# create a vector x <- c(1:6) # designate the dimensions, creating a matrix dim(x) <- c(3, 2)
Lets inspect our matrix.
# notice the matrix is filled column-wise! x
## [,1] [,2] ## [1,] 1 4 ## [2,] 2 5 ## [3,] 3 6
# show no of rows in x nrow(x)
## [1] 3
# show no of columns in x ncol(x)
## [1] 2
We could now create a 3-D array using the same assignment technique as above;
Note: As geoscientists we often deal with 3D dimensional data and so it can be useful to simultaneously think in terms of x, y and z (or t) space when constructing arrays.
# array with 1 row, 2 columns and 3 'levels' dim(x) <- c(1, 2, 3) x
## , , 1 ## ## [,1] [,2] ## [1,] 1 2 ## ## , , 2 ## ## [,1] [,2] ## [1,] 3 4 ## ## , , 3 ## ## [,1] [,2] ## [1,] 5 6













