User Tools

Site Tools


en:courses:training:element-01:lecture-notes:lc-ln-01

L01: Welcome back to R

“Here comes a headache”

Kevin Flynn, Tron

Things we cover in this session

  • How to read csv files
  • First visualizations and summary analysis

Things you need for this session

Things to take home from this session

At the end of this session you should be able to

  • write and save a simple R script in R studio
  • execute individual lines of an R script in R studio

Say hello to R

Hello world

For interactive scripting, you need to type the R commands directly into an R console. An R console opens automatically when you type “R” into a DOS or Linux shell provided that you have installed R on your system before. Try the mother of all examples - type the following in your R console and press Enter:

print("Hello World")

Alternatively, you can assign the string to a variable using “←” which is R's version of “=”. Afterwards, you can print the content of the variable (note, this time, no quoting inside the print function since you call a variable, not a string):

my_statement <- "Hello World"
print(my_statement)

Arithmetic operations

Of course, you can not only work with characters/strings but also with numbers. R has been invented for statistical computing, so there is a lot of stuff available. For now, we just introduce the basic arithmetic operators:

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation
** Exponentiation
%% Modulus
%/% Integer Division

Beside these operators, R has many built-in functions like mean, median, square root, absolute value etc. An overview can be found at e. g. Quick R.

Libraries

R offers a large functionality. This functionality is accessible through so called R packages which is the same as a library in any other programming language. To load an R package into your script so you can access the functions within it, just type the following in your R console or script:

library(<name of the library>)

If you want to use a library for the first time (on your system), it is very likely that you have to install it first. This is done by typing the following into an R console:

install.packages("<name of the library>")

Please note that if you want to load a library, you must not use “” inside the library() function. If you want to install a library, you have to put the name into “” inside the install.packages() function.

For more information on installing R packages, visit this tutorial from R-bloggers.

More information

For more information and general help on R, visit the R project homepage where you e.g. also find manuals. If you need help for a specific package, have a look at the documentations of the standard library and the contributing packages.

If you need help on a particular package, just type ?<package_name> in the R console. If you don't know which function you need, Google it (and look for results on the stackoverflow.com page)

R Studio

R Studio

Executing code in the R shell is good, using R Studio is superior. Along with Eclipse which is the standard choice for Java, Python or C programming, R Studio is one of the prime integrated development environments. Install it, run it, love it.

Basically, R Studio has four window frames. The upper left one is the standard frame for writing code. Below is the console which shows what happens, if the code is executed. Of course one can also directly type something into the console window (which is just the same R environment as the one which can be started from the operating system's bash). The upper right window displays all variables/data sets which have been used during code execution. Finally, the lower right window serves multiple purposes. Two nice features are the Help tab which displays the content of the R documentation of the respective package (just type ?<package_name in the console window) and the Plots tab which shows the results from any executed visualization command.

To execute code written in the upper left window, just select the respective lines and press <Cntrl>+<Return>

For more information see R Studio's documentation.

Time for practice

en/courses/training/element-01/lecture-notes/lc-ln-01.txt · Last modified: 2023/08/07 15:41 by mludwig