After reading and practicing with the Introducction to R and RStudio material, we can go through more R and RStudio tools.
The Scripts are text files that save series of instructions (as the ones we practiced in the previous section) and then they can be executed. These scripts are usefull when you need to excecute a program full of those instructions, also you can share and carry these scripts.
In order to create a R script in RStudio we need to go to the File option, then New File and select R Script (as we can see in the first image). Another way to do it is going to the tool bar in the RStudio interface and click on the white sheet with a plus sign \(+\) and then select R Script (as we can see in the second image). Finally, the easiest way to create a new script is with the ctrl+shift+N shortcut.
After creating our first Script we are going to use the following code to prove it.
x = 0:99 * pi/50 # 100 points between 0 and 2pi
y = sin(x) # The sine function evaluated in x
plot(x,y) # A simple visualization of the sine function
RStudio has a lot of ways to execute commands in a Script. For example, if we want to execute one code line we use the ctrl+Enter shortcut; if we want to execute more lines we can select them and use the same shortcut and finally, if we want to execute the whole Script we just need to click the source button.
A dataframe is a data structure similar to a matrix but the main difference is that you can use other data type in the columns, we can see it with an example using a dataframe that R has by default and it is called mtcars.
data(mtcars) # Load the data from mtcars
head(mtcars) # display the fisrt 6 elements of the dataframe
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Now let’s explore this dataframe.
nrow(mtcars) # Number of rows
## [1] 32
ncol(mtcars) # Number of columns
## [1] 11
?mtcars
In order to access a dataframe’s column we can do the following:
mean(mtcars$hp) # Mean of the gross hoursepower
## [1] 146.6875
sum(mtcars[9]) # Number of motors with manual transmission
## [1] 13
We can also see with a graphic how the miles per galon are distributed in function of the horse power.
plot(mtcars$hp,mtcars$mpg)
Para construir nuestro propio dataframe podemos usar la funcion data.frame, por ejemplo:
names = c("Rodolfo", "Jhon", "Carla","Maria");
age = c(32,45,28,30);
data <- data.frame(names,age);
head(data)
## names age
## 1 Rodolfo 32
## 2 Jhon 45
## 3 Carla 28
## 4 Maria 30
The conditionals are a set of instructions that are executed depending on the boolean value of the initial condition. For example, if we want to know of a number is greater than 5 we can do the following:
myNum = 7;
if (myNum > 5){ # IF (condition) {
"This number is greater than 5" # instructions to do if the condition is true
} # } The {} defines the start and end of the instructions
## [1] "This number is greater than 5"
In case that the condition is false the previous program won’t do anything, to add an instruction to be executed in this case we can use else at the end of the last instructions.
myNum = 7;
if (myNum > 5){ # IF (condition) {
"This number is greater than 5" # Commands to do if the condition is true
}else{ # else{
"This number isn't greater than 5" # Comands to do if the condition id false
} # }
## [1] "This number is greater than 5"
If the condition we want to evaluate is more complex we inclue else if, in this case the last else will only be executed if all the previous conditions are false, in case when two conditions are true just the first one will be eecuted.
myNum = 6
if (myNum < 5 & myNum > 2){
"This number is greater than 2 and less than 5"
}else if(myNum > 5 & myNum != 6){
"This number is greater than 5, but different from 6"
}else{
"This number is less than 2 or is 6"
}
## [1] "This number is less than 2 or is 6"
The loops are functions that execute a set of commands in a repeated way. The simple one is the for loop which executes those commands a certain number of times, here we have an example:
days = c('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
for(i in 1:7){ # i is a variable that changes its value from 1 to 7
days[i];
}
Here we have another way to do the previous loop
for(day in days){
day
}
Finally this is the while function which blends the two ideas we just saw beacuse it executes commands repeatedly until it reaches a false condition.
x = c(3,5,2,9,7,1,4,8,9)
index = 1;
while(x[index] != 1){
index = index + 1;
}
index
## [1] 6
Note: be careful when you use this function because you could use a contition that never occurs and then the execution will loop forever.
The functions are an easy way to define a new instruction form others that are already created, for example if we want to define a function that returns the fibbonacci series until a number \(n\) we can do the following:
Fib <- function(n){ # we define the Fib function with parameter n
if(n == 1){
return (0); # return is used to get the function's result, after executing the return the function stops executing the other functions
}else if(n == 2){
return (1);
}
x = 0;
y = 1;
for(i in 1:n-2){
tmp = x;
x = y;
y = tmp + y;
}
return (y);
}
To use execute the instructions in the function we have to use the name of the funtion and then add the parameters, in this case is just one number, if we had more we would have to separate them with a coma.
Fib(6)
## [1] 13
The packages are sets of functions an variables already created by the R community and developers in order to be used as tools. Thes packages are very useful to add functionalities that R doesn’t have by default. These are easy to install, we just need to use the install.packages() comand and type the name of the library that we want to install between the brackets. For example, if we want to see our graphics prettier we can install the ggplot2 library
install.packages('ggplot2')
## Installing package into '/home/and/R/x86_64-pc-linux-gnu-library/4.0'
## (as 'lib' is unspecified)
To use it we have to tell R that we are including it in our workspace, like this
library(ggplot2)
Let’s do a trial with the dots in the datafram we saw earlier:
ggplot() + geom_point(aes(x = mtcars$hp,y = mtcars$mpg))
The graphics in this library have a lot of options to personalize them and some other tools to see the data contained in a data.frame.