Welcome everyone to learning R. This course entails a complete run down on the basics of programming with statitical software package R.
This course assumes you have little to no prior knowledge in programming and will help with building the fundermentals required for the use of the R software package and the basics required should further learning towards advanced programming with R and other programming languages.
The break down of the course is as followed;
The prupose of this course to ensure that upone your completion of this course you will be able to tackle problems you would face during your studies or industry. The notes for this course will be provided to you so that you will be able to refer to them for future refernce.
This course will focus towards the use of using R studio. To install R studio, we require the installation of R and R studio. Below constains the links for first installing r on your computer. It is important tthat you select the correct operating system for you computer. Importantly when installing ensure that you uncheck the uneccacry version as installing future packages can lead to problems requiring you to reinstall from the begining
Now we can begin looking at the interface of R
Our first few approaches will be the basic of using the R console
1+1
## [1] 2
As we can see the result is 2, now we can try subtraction
5-3
## [1] 2
Similar we get 2. For multiplication and division we have the following symbols to use respectively \(*\) and \(/\).
5*3
## [1] 15
10/2
## [1] 5
we can also read in text values
"hello world"
## [1] "hello world"
However, we must understand how R views this kind of imput to check this we can use a special base function, a pre defined function that is pre loadedwhen you start R. This code can be applied to any form of input data even for numerical values.
This can also be done for logical values such as true and false.
is.character("hello world")
## [1] TRUE
is.numeric("hello world")
## [1] FALSE
is.logical(TRUE)
## [1] TRUE
Writing value one after another can be time consuming, hence we can useloops to iterate over large data sets. To explain this we will use the following example. Suppose you would like iterate over a rnage of numbers from 1 to 20 you could manually type out each value or use a loop. Since the number of interest range from 1 to 20 we can specify the range for which the loop is to iterate over.
for (i in 1:20){
print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15
## [1] 16
## [1] 17
## [1] 18
## [1] 19
## [1] 20
We will take a moment hear ton undertand carefully what has happened here. We have stated that we would like to loop over the range 1 to 20. We have done this by, using the command \(for\) then opening the brackets, then we must create a varaible variable for which we can iterate the loop over, make sure you are using a new varaible as using the same varailbe that was previously stated will cause issues unless it is assigned in a function,
Now to see what is happening to i in the for loop we, then print i to reflect the change. It should be notted that the value of i is increasing by 1 each time for 20 iterations. This is the most basic comphrehension of a for loop in R. Importantly we can do lots of work using for loops as we will learn later it is possible to to use for loops to iterarte very complex functions. Of course there are correct and incorrect methodes to apply for loops which we will learn later.
As is shown we have a range of number from 1 to 20. We can event take this a little further and specifiy the step size of each increment. Suppose we want to count by step of 3, we can do this by setting the step size = \(3\). We do this by replacing the : with the function sequnece which is known as \(seq()\). This is found with in the help window in the bottom right which will call the R documentation which we will use later.
Question, what do we expect the last value to be for this for loop?
for (i in seq(1,20,3)){
print(i)
}
## [1] 1
## [1] 4
## [1] 7
## [1] 10
## [1] 13
## [1] 16
## [1] 19
Correct, when using the sequence function we need to understand that we are the ones sepcifying the start and stop functions. But lets upoose we want to stop the loop running after we hit particular threshold or value. We can achieve this with another looping function called a while loop. So a while loop keeps on iterating until a set condition has been satisfied.
It must be noted that should your for loop statment ever reach the desired condition it will keep on iterating. To show you this we will write a very basic function
#i = 1
#while( i == 1)
#{
# print(i)
#}
The above case is the perfect example of when a condition is lways meet for which the loop will continue until the computer crashes or is terminated early.
i = 1
w = 0
while( i == 1 & w <= 20){
w = w+1
print(w)}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15
## [1] 16
## [1] 17
## [1] 18
## [1] 19
## [1] 20
## [1] 21
the last fundermental component of r is logic statments. Logic stements will return a true of false whena particualr condition has been meet. A simple example is the case where we have \(6 > 3\). Vice verssa we get the opposite when we chnage the equation heads around and have a less than sign. However let look at the case where we use an unassigned varaible \(a\) and use the arguement that a is greater than \(3\). We get the an error since the value of a has no value associated to it.
6 > 3
## [1] TRUE
6 < 3
## [1] FALSE
#a > 3
however we do run into an issue were we compare a character to a numerical value. As can be seen in the below code we have three arguments being cheked;
"a" > 3
## [1] TRUE
"a" <= "b"
## [1] TRUE
"a" >= "A"
## [1] FALSE
another methode of writing a logic stement is using the if function. However unlike the previous statents, we can specify a stament to be run should the logic stament be satisfied.
if(2 > 1){
print("hello")
}
## [1] "hello"
However suppose we would like two results when a condition is satisfied and not satisfied or a more extreme case where we have multiple condtions to be satisfied. We approach this by using two additional statements else if and else. For this we will assign j equal to 5, we will then create 2 conditions, 1, where we check if 5 is whole divisable by 2, and the other condition where we check if 5 is a negative number else we will print the messge that j is is neither of these conditions
j = 5
if(round(j/2) == j/2){
print("statement 1 is true")
} else if(j < 0) {
print("second statement is true")
} else {
print("neither of these conditions are true")
}
## [1] "neither of these conditions are true"
So to wrap up this we will combine everything we have learned so far in this chapter together. we will builda for loop and iterate over 1 to 100 and return only even numbers.
for (i in 1:100){
if (round(i/2) == i/2 )
print(i)
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
## [1] 12
## [1] 14
## [1] 16
## [1] 18
## [1] 20
## [1] 22
## [1] 24
## [1] 26
## [1] 28
## [1] 30
## [1] 32
## [1] 34
## [1] 36
## [1] 38
## [1] 40
## [1] 42
## [1] 44
## [1] 46
## [1] 48
## [1] 50
## [1] 52
## [1] 54
## [1] 56
## [1] 58
## [1] 60
## [1] 62
## [1] 64
## [1] 66
## [1] 68
## [1] 70
## [1] 72
## [1] 74
## [1] 76
## [1] 78
## [1] 80
## [1] 82
## [1] 84
## [1] 86
## [1] 88
## [1] 90
## [1] 92
## [1] 94
## [1] 96
## [1] 98
## [1] 100
From this chapter we have gained a very basic understanding of how functions and operates. The basic understanding of for loops and logic statments. We have also combined all of these together to produce a very simple algorithium to that return all the even numbers between 1 to 100. In the next chapter we will be looking at creating scripits installing packages and using libraries. We will also desgin our first function in R