S
R is a modern language with a rather short history. In 1992, the R-project was started by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand. The first version was available in 1995 and the first stable version was available in 2000.
Now, the R Development Core Team (of which Chambers is a member) develops R further and maintains the code. Since a few years Microsoft has embraced the project and provides MRAN (Microsoft R Application Network). This package is also free and open source software (FOSS) and has some advantages over standard R such as enhanced performance (e.g. multi-thread support, the checkpoint package that makes results more reproducible).
FOSS
Essentially, R is …
a programming language built for statistical analysis, graphics representation and reporting;
an interpreted computer language which allows branching, looping, modular programming as well as object and functional oriented programming features.
R offers its users …
integration with the procedures written in the C, C++, .Net, Python, or FORTRAN languages for efficiency;
C
C++
.Net
Fortran
zero purchase cost (available under the GNU General Public License), and pre-compiled binary versions are provided for various operating systems like Linux, Windows, and Mac;
Linux
Windows
Mac
simplicity and effectiveness;
a free and open environment;
an effective data handling and storage facility;
a suite of operators for calculations on arrays, lists, vectors, and matrices;
a large, coherent, and integrated collection of tools for data analysis;
graphical facilities for data analysis and display either directly at the computer or printing;
a supportive on-line community;
the ability for you to stand on the shoulders of giants (e.g. by using libraries).
R is arguably the most widely used statistics programming language and is used fromuniversities to business applications, while it still gains rapidly in popularity.
Hint – Getting more help
If at any point you are trying to solve a particular issues and you are stuck, the online community will be very helpful. To get unstuck, do the following:
First, look up your problems by adding the keyword “R” in the search string. Most probably, someone else encountered the very same problem before you, and the answer is already posted. Avoid to post a question that has been answered before.
If you need to ask your question in a forum such as for example www.stackexchange.comthen you will need to add a minimal reproducible example. The package reprex can help you to do just that.
4.1 Getting Started with R
Before we can start, we need a working installation of R on our computer. On Linux, this can be done via the command line. On Debian and its many derivatives such as Ubuntu or Mint, this looks as follows: 1
installing R
sudo apt-get install r-base
On Windows or Mac, you want to refer to https://cran.r-project.org
and download the right package for your system.
To start R, open the command line and type R
(followed by enter). This is the R interpreter (or R console). You can do all your data crunching here. To leave the environment type q()
followed by [enter].
Hint – Using R Online
It is also possible to use R online:
https://www.tutorialspoint.com/execute_r_online.php
http://www.r-fiddle.org
For the user, who is not familiar with the command line, it is highly recommendable to use an IDE, such as RStudio (see https://www.rstudio.com
). Later on – for example in Chapter 32“ R Markdown ” on page 879 – we will see that RStudio has some unique advantages over the R-console in store, that will convince even the most traditional command-line-users.
IDE
RStudio
Whether you use standard R or MRAN, using RStudio will enhance your performance and help you to be more productive.
MRAN
Rstudio is an integrated development environment (IDE) for R and provides a console, editor with syntax-highlighting, a window to show plots and some workspace management.
IDE
integrated development environment
Hint – RStudio is free
Rstudio can be downloaded from https://www.rstudio.com
for free.
RStudio is also in the repositories of most Linux distributions. That means that there is no need to go to the website or RStudio, but it is possible to download R, and install it with just one line in the CLI of your OS. For example, in Debian and its derivatives, this would be:
# Note that the first 2 lines are just to make sure that # you are using the latest version of the repository. sudo apt-get update sudo apt-get upgrade # Install RStudio: sudo apt-get install rstudio
This process is very simple: provide your admin password and the script will take care of everything. Then use your preferred way to start the new software. R can be started by typing R
in the command prompt and RStudio will launch with the command rstudio
.
An important note is that RStudio will work with the latest version of R that is installed. Also when you install MRAN, RStudio will automatically load this new version.
The basic operators work as one would expect. Simply type in the R terminal 2+3
followed by ENTER
and R will immediately display the result.
addition product power
#addition2 +3 #product2 *3 #power2 **3 2 ∧3 #logic2 <3 x <- c(1,3,4,3) x.mean <- mean(x) x.mean y <- c(2,3,5,1) x +y
Hint – White space
Usually, white space is not important for R.
For example, mean ( c (c + pi, 2 ) )
will do exactly the same as mean(c(1+pi,2))
. However, it is a good habit to add a white space before and after operators and after the comma. This will improve readability, which makes the debugging process easier and is helpful for other people who want to read your code.
Editing variables interactively
To create a variable x
, type:
scan()
x <- scan()
will start an interface that invites you to type all values of the vector one by one.
In order to get back to the command prompt: type enter without typing a number (ie. leave one empty to end).
Further information – Other ways to import data
This is only one of the many ways to get data into R. Most probably you will use a mix of defining variables in the code and reading in data from files. See for example Chapter 15“ Connecting R to an SQL Database ” on page 327.
Читать дальше