R Studio is a multi-platform integrated development environment (IDE) specialised for R programming. R studio can be downloaded and installed from this website: https://www.rstudio.com/. The software is open-sourced and can be downloaded free. An installer suitable for an operating system needs to be downloaded, otherwise no installation occurs.
source: https://www.rstudio.com/products/rstudio/download/#download
Please note: The R framework needs to be installed separately from R Studio (see What is R programming language?) |
The R studio has four quadrants:
Section | Purpose |
---|---|
Top left | R scripts open, and are available to be edited and saved. |
Top right | The Environment lists all the variables and functions defined and used in a session. History lists all the command typed in the console. Connection can help you to connect to a database, it is an advanced feature.. |
Bottom left | The R console allows running some commands directly after the cursor |
Bottom right | The File explorer is a file-management tool. The plotting screen shows graphs being plotted. The packages lists allows to loads and attach adds-on packages. The help provide some useful information about some functions provided by some packages and the R languages. |
Some output example from the console:
print("hello world!") [1] "hello world!" |
The console lets typing some R commands. The results can be viewed quickly in the history and environment. The work is likely not to be saved once R Studio is closed or a session is finished. You will need to type some code next to do > cursor.
type print("hello world!")
print("hello world!") |
Beginners and not-quite advanced readers
Before continuing with this part of the practice, you should read the sections on data types and variables (see reading list). It should clarify the concepts of data types as well as variables in R. Some interactive and live demo can be edited to deepen your learning.
Logical variables also known as Boolean variables
Integer and decimal variables
Character variables
Vector and lists variables
Additional reading list |
---|
Beginners and not-quite advanced readers
Before continuing with this part of the practice, you should read the sections on operators (see reading list). It should clarify the concepts of data types as well as variables in R. Some interactive and live demo can be edited to deepen your learning.
Arithmetical operators
R is not strongly typed. For that reasons, a variable created as a logical can become a character. It can break many calculations and it is not advisable to use. However, it is worth exploring what is happening.
Scripts can permanently save some R commands and R programs. The latter are often structured, so that they can be understood without any confusion. A script is a record of the analyses that are completed and typically will run again. Scripts often tell a story with a clear start and clear end. At the start, we obtain some data before analysing them with some R functions. Then we show the results in a clear manner with a variety of graphical representation.
To make your script clear and easy to use again, it helps to do the following:
#
at the beginning of each line. Open up a new script by selecting from the top left icon for New File
.R
file in your folder by clicking the save icon ( a blue disk). For this exercise, some temperatures for a whole year will be randomly generated and stored in a vector. Some statistical analysis is completed before showing the results of the analysis. We will be using some statistical function already implemented in R. Finally, commenting will be encouraged to communicate the structure and ideas used in the script.
Beginners: Write a simple script with some comments, using the some examples from the tutorial. It would be a good thing to use some graphical representation using the functions plot or hist.
Not quite advanced users: Write a more complex script and try to complete some calculations using the results of the statistical functions. Have you considered to using a range of parametric and non-parametric statistical methods.
Advanced users: Write a script with several levels of analysis. Considering and verifying the data is clean (i.e., no non-numerical in the vector) would be a good thing to do.
An incomplete script could look in this manner.
Example of script
The outcome should be similar as this....
print(paste("minimum value", min)) [1] "minimum value -5" print(paste("maximum value", max)) [1] "maximum value 30" print(paste("average ", average)) [1] "average value 12.3150684931507" |
Read again your script and comment each section, with some explanations.
Before continuing with this part of the practice, you should read the sections related to decision making and function(see reading list). It should clarify the concepts of data types as well as variables in R. Some interactive and live demo can be edited to deepen your learning. It is advisable to practice in the live demo the following R statements: if, if ...else ..., switch. You should also use the help to find more about the stop function.
Additional reading list |
---|
Code to be typed:
The results should show the following results. The source function was invoked and all the code was interpreted, lines by lines. The first division was possible and the code within the if statement was not executed. The second division was not completed. the condition of the if statement was met. The stop function interrupted the execution of the script altogether.
[1] 1 Error in eval(ei, envir) : you cannot divide by 0! |
The above script has a lot of repeated code, which is an undesirable feature to any script. It is more challenging to maintain. For that reason, it is better to promote code re-use.
In the console, test your function with a range of values for the numerator and the denominator.
The output should look in this manner ...
result1 <- divide(5,0) Error in divide(5, 0) : you cannot divide by 0! result2 <- divide(78,4) print(result2) [1] 19.5 |
Vectors, Lists, Data frames and Tibbles