R For Dummies. Vries Andrie de

Чтение книги онлайн.

Читать онлайн книгу R For Dummies - Vries Andrie de страница 5

R For Dummies - Vries Andrie de

Скачать книгу

These vectors allow you to perform sometimes complex operations on a set of values in a single command.

Performing multiple calculations with vectors

      R is a vector-based language. You can think of a vector as a row or column of numbers or text. The list of numbers {1,2,3,4,5}, for example, could be a vector. Unlike most other programming languages, R allows you to apply functions to the whole vector in a single operation without the need for an explicit loop.

      It is time to illustrate vectors with some real R code. First, assign the values 1:5 to a vector called x:

      > x <– 1:5

      > x

      [1] 1 2 3 4 5

      Next, add the value 2 to each element in the vector x:

      > x + 2

      [1] 3 4 5 6 7

      You can also add one vector to another. To add the values 6:10 element-wise to x, you do the following:

      > x + 6:10

      [1] 7 9 11 13 15

      To do this in most other programming language would require an explicit loop to run through each value of x. However, R is designed to perform many operations in a single step. This functionality is one of the features that make R so useful – and powerful – for data analysis.

      We introduce the concept of vectors in Chapter 2 and expand on vectors and vectorization in much more depth in Chapter 4.

Processing more than just statistics

      R was developed by statisticians to make statistical data analysis easier. This heritage continues, making R a very powerful tool for performing virtually any statistical computation.

      As R started to expand away from its origins in statistics, many people who would describe themselves as programmers rather than statisticians have become involved with R. The result is that R is now eminently suitable for a wide variety of nonstatistical tasks, including data processing, graphical visualization, and analysis of all sorts. R is being used in the fields of finance, natural language processing, genetics, biology, and market research, to name just a few.

      

R is Turing complete, which means that you can use R alone to program anything you want. (Not every task is easy to program in R, though.)

      In this book, we assume that you want to find out about R programming, not statistics, although we provide an introduction to statistics with R in Part IV.

Running code without a compiler

      R is an interpreted language, which means that – contrary to compiled languages like C and Java – you don’t need a compiler to first create a program from your code before you can use it. R interprets the code you provide directly and converts it into lower-level calls to pre-compiled code/functions.

      In practice, it means that you simply write your code and send it to R, and the code runs, which makes the development cycle easy. This ease of development comes at the cost of speed of code execution, however. The downside of an interpreted language is that the code usually runs slower than the equivalent compiled code.

      

If you have experience in other languages, be aware that R is not C or Java. Although you can use R as a procedural language such as C or an object-oriented language such as Java, R is mostly based on the functional programming paradigm. As we discuss later in this book, especially in Part III, this characteristic requires a bit of a different mindset. Forget what you know about other languages, and prepare for something completely different.

Chapter 2

      Exploring R

In This Chapter

      ▶ Looking at your R editing options

      ▶ Starting R

      ▶ Writing your first R script

      ▶ Finding your way around the R environment

      In order to start working in R, you need two things. First, you need a tool to easily write and edit code (an editor). You also need an interface, so you can send that code to R. Which tools you use depend to some extent on your operating system. The basic R install gives you these options:

      ✔ Windows: A basic user interface called RGui.

      ✔ Mac OS X: A basic user interface called R.app.

      ✔ Linux: There is no specific interface on Linux, but you can use any code editor (like Vim or Emacs) to edit your R code. R itself opens by default in a terminal window.

      At a practical level, this difference between operating systems and interfaces doesn’t matter very much. R is a programming language, and you can be sure that R interprets your code identically across operating systems.

      Still, we want to show you how to use a standard R interface, so in this chapter we briefly illustrate how to use R with the Windows RGui. Our advice also works on the Mac R.app.

      Fortunately, there is an alternative, third-party interface called RStudio that provides a consistent user interface regardless of operating system. RStudio increasingly is the standard editing tool for R, so we also illustrate how to use RStudio.

      In this chapter, after opening an R console, you flex your R muscles and write some scripts. You do some calculations, create some numeric and text objects, take a look at the built-in help, and save your work.

      Working with a Code Editor

      R is many things: a programming language, a statistical processing environment, a way to solve problems, and a collection of helpful tools to make your life easier. The one thing that R is not is an application, which means that you have the freedom of selecting your own editing tools to interact with R.

      In this section we discuss the Windows R interface, RGui (short for R graphical user interface). This interface also includes a very basic editor for your code. Since this standard editor is so, well, basic, we also introduce you to RStudio. RStudio offers a richer editing environment than RGui and many handy shortcuts for common tasks in R.

      Alternatives to the standard R editors

      Among the many freedoms that R offers you is the freedom to choose your own code editor and development environment, so you don’t have to use the standard R editors or RStudio.

      These are powerful full-featured editors and development environments:

      ✔ Eclipse StatET (www.walware.de/goto/statet): Eclipse, another powerful integrated development environment, has an R add-in called StatET. If you’ve done software development on large projects, you may find Eclipse

Скачать книгу