User Tools

Site Tools


en:learning:schools:s01:lecture-notes:ba-ln-01

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:learning:schools:s01:lecture-notes:ba-ln-01 [2015/09/22 16:22] (current)
Line 1: Line 1:
 +====== L01: Welcome to R ======
 +
 +"Here comes a headache"​
 +
 +Kevin Flynn, Tron
 +
 +==== Things we cover in this session ====
 +  * What is R
 +  * What is an R script and how it is executed?
 +  * Using R Studio as editor
 +
 +==== Things you need for this session ====
 +  * [[en:​learning:​schools:​s01:​worksheets:​ba-ws-01-1|W01-1:​ R studio]]
 +==== 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:
 +
 +<code rsplus> ​
 +print("​Hello World"​)
 +</​code> ​
 +
 +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):
 +
 +<code rsplus> ​
 +my_statement <- "Hello World"
 +print(my_statement)
 +</​code> ​
 +
 +==== 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. [[http://​www.statmethods.net/​management/​functions.html|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:
 +<code rsplus>
 +library(<​name of the library>​)
 +</​code>​
 +
 +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:
 +<code rsplus>
 +install.packages("<​name of the library>"​)
 +</​code>​
 +
 +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 [[http://​www.r-bloggers.com/​installing-r-packages/​|this tutorial]] from R-bloggers.
 +==== More information ====
 +
 +
 +For more information and general help on R, visit the [[http://​cran.r-project.org/​|R project homepage]] where you e.g. also find [[http://​cran.r-project.org/​manuals.html|manuals]]. If you need help for a specific package, have a look at the documentations of the [[http://​stat.ethz.ch/​R-manual/​R-patched/​doc/​html/​packages.html|standard library]] and the [[http://​cran.r-project.org/​web/​packages/​|contributing packages]].
 +<note tip>
 +
 +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)</​note>​
 +
 +
 +
 +===== R Studio =====
 +
 +[[http://​www.rstudio.com/​ide/​|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.
 +
 +<note tip>To execute code written in the upper left window, just select the respective lines and press ''<​Cntrl>​+<​Return>''</​note>​
 +
 +For more information see R Studio'​s [[https://​support.rstudio.com/​hc/​en-us/​categories/​200035113-Documentation|documentation]].
 +
 +===== Time for practice =====
 +[[en:​learning:​schools:​s01:​worksheets:​ba-ws-01-1|W01-1:​ R studio]]
  
en/learning/schools/s01/lecture-notes/ba-ln-01.txt ยท Last modified: 2015/09/22 16:22 (external edit)