R is an extremely popular programming language for statisticians, social science and data scientists. It ranks 5th of the most popular languages in general, it is open source and full with helpful and mostly user written packages. When you want to become a data scientist, it is one of the go to languages together with Python. THE most common GUI (graphical user interface) for R is R-Studio and both the language and the GUI are easy to install.
In order to install R properly, go to the R-webpage and select your operating system. Download and install it like any other program. On top of your brand new R distribution, install R-Studio, downloadable here — and yes here we go, you just installed you first programming language and are ready to use it.
On first start, R-Studio will automatically check for your R distribution, it usually recognizes it and you are ready to go. You may want to change some general settings under R-Studio -> preferences.
I recommend to set a default working directory and to uncheck Restore .RData into workspace at startup. Checking this would R-Studio reload all your objects of a previous session everytime you start. This is often unnecessary.
The R-Studio Layout
The layout is quite simple, you can find your R-Script in the top left corner. You put your code in here and save it to continue your work later. The most useful shortcuts on Mac are:
- Execute the code CMD+Enter
- Block comment CMD+Shift+C
- Undo and redo are as usual CMD+Z and CMD+Y
You can find the windows shortcuts here.
The second important window is on the top right. You will see all of your objects/functions and data which are loaded in your current environment. It is extremely helpful and you can find necessary information about your objects here. Clicking on the objects let you inspect them. On the lower left side you find the R Console. All the code you have executed can be found here. Additionally, you will see the feedback R is giving you. So if you get any error/warning messages the feedback will be displayed here. Finally, on the lower right corner is a multi-propose window. It displays the plots you have created, the documentation, the libraries (packages and content for R created by others) and your files.
The very Beginning
R makes use of two important concepts the workspace and the working directory. The workspace is a place where R saves all the objects containing your data, functions you want to use for a specific project, the parameters of an estimated regression, so everything you have created. The working directory is used to specify a place where all your code/data/plots etc. should go if you do not specify it differently — so it makes it faster to save your stuff.
save.image("/Users/UserName/R/MyFirstProject.RData") #Example for a Mac to save your workspace. Do not forget the .RData
getwd() # will display your current working directory in the console
setwd("/Users/YourName/TheFolderYouWant/") #when you specify a new wd you need to use "/", or "\\" on Windows.
You just used your first command in R, congratulations! When you see a command and you do not know it, there is no need to google it. You can simply type:
R-Studio will bring up the documentation for this command on the lower right corner.
Additional to a description of the command you will find its usage, the parameters you can specify, details and examples. Especially interesting for functions are the returning values. I can’t stress how helpful this functionality is in R-Studio. You should get used to it to use it before you ask Google or Stackoverflow for help.