Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $9.99/month after trial. Cancel anytime.

R For Dummies
R For Dummies
R For Dummies
Ebook568 pages7 hours

R For Dummies

Rating: 4 out of 5 stars

4/5

()

Read preview

About this ebook

Master the programming language of choice among statisticians and data analysts worldwide

Coming to grips with R can be tough, even for seasoned statisticians and data analysts. Enter R For Dummies, the quick, easy way to master all the R you'll ever need. Requiring no prior programming experience and packed with practical examples, easy, step-by-step exercises, and sample code, this extremely accessible guide is the ideal introduction to R for complete beginners. It also covers many concepts that intermediate-level programmers will find extremely useful.

  • Master your R ABCs ? get up to speed in no time with the basics, from installing and configuring R to writing simple scripts and performing simultaneous calculations on many variables
  • Put data in its place ? get to know your way around lists, data frames, and other R data structures while learning to interact with other programs, such as Microsoft Excel
  • Make data dance to your tune ? learn how to reshape and manipulate data, merge data sets, split and combine data, perform calculations on vectors and arrays, and much more
  • Visualize it ? learn to use R's powerful data visualization features to create beautiful and informative graphical presentations of your data
  • Get statistical ? find out how to do simple statistical analysis, summarize your variables, and conduct classic statistical tests, such as t-tests
  • Expand and customize R ? get the lowdown on how to find, install, and make the most of add-on packages created by the global R community for a wide variety of purposes
  • Open the book and find:
  • Help downloading, installing, and configuring R
  • Tips for getting data in and out of R
  • Ways to use data frames and lists to organize data
  • How to manipulate and process data
  • Advice on fitting regression models and ANOVA
  • Helpful hints for working with graphics
  • How to code in R
  • What R mailing lists and forums can do for you

LanguageEnglish
PublisherWiley
Release dateJun 6, 2012
ISBN9781119963134
R For Dummies

Related to R For Dummies

Related ebooks

Related articles

Reviews for R For Dummies

Rating: 3.7999999866666667 out of 5 stars
4/5

15 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    R For Dummies - Andrie de Vries

    Part I

    R You Ready?

    9781119962847-pp0101.eps

    In this part . . .

    From financial headquarters to the dark cellars of small universities, people use R for data manipulation and statistical analysis. With R, you can extract stock prices and predict profits, discover beginning diseases in small blood samples, analyze the behavior of customers, or describe how the gray wolf recolonized European forests.

    In this part, you discover the power hidden behind the 18th letter of the alphabet.

    Chapter 1

    Introducing R: The Big Picture

    In This Chapter

    arrow Discovering the benefits of R

    arrow Identifying some programming concepts that make R special

    With an estimated worldwide user base of more than 2 million people, the R language has rapidly grown and extended since its origin as an academic demonstration language in the 1990s.

    Some people would argue — and we think they’re right — that R is much more than a statistical programming language. It’s also:

    check.png A very powerful tool for all kinds of data processing and manipulation

    check.png A community of programmers, users, academics, and practitioners

    check.png A tool that makes all kinds of publication-quality graphics and data visualizations

    check.png A collection of freely distributed add-on packages

    check.png A toolbox with tremendous versatility

    In this chapter, we fill you in on the benefits of R, as well as its unique features and quirks.

    tip.eps You can download R at www.r-project.org. This website also provides more information on R and links to the online manuals, mailing lists, conferences and publications.

    Tracing the history of R

    Ross Ihaka and Robert Gentleman developed R as a free software environment for their teaching classes when they were colleagues at the University of Auckland in New Zealand. Because they were both familiar with S, a commercial programming language for statistics, it seemed natural to use similar syntax in their own work. After Ihaka and Gentleman announced their software on the S-news mailing list, several people became interested and started to collaborate with them, notably Martin Mächler.

    Currently, a group of 18 people has rights to modify the central archive of source code. This group is referred to as the R Development Core Team. In addition, many other people have contributed new code and bug fixes to the project.

    Here are some milestone dates in the development of R:

    check.png Early 1990s: The development of R began.

    check.png August 1993: The software was announced on the S-news mailing list. Since then, a set of active R mailing lists has been created. The web page at www.r-project.org/mail.html provides descriptions of these lists and instructions for subscribing. (For more information, turn to It provides an engaged community, later in this chapter.)

    check.png June 1995: After some persuasive arguments by Martin Mächler (among others) to make the code available as free software, the code was made available under the Free Software Foundation’s GNU General Public License (GPL), Version 2.

    check.png Mid-1997: The initial R Development Core Team was formed (although, at the time, it was simply known as the core group).

    check.png February 2000: The first version of R, version 1.0.0, was released.

    Ross Ihaka wrote a comprehensive overview of the development of R. The web page http://cran.r-project.org/doc/html/interface98-paper/paper.html provides a fascinating history.

    Recognizing the Benefits of Using R

    Of the many attractive benefits of R, a few stand out: It’s actively maintained, it has good connectivity to various types of data and other systems, and it’s versatile enough to solve problems in many domains. Possibly best of all, it’s available for free, in more than one sense of the word.

    It comes as free, open-source code

    R is available under an open-source license, which means that anyone can download and modify the code. This freedom is often referred to as free as in speech. R is also available free of charge — a second kind of freedom, sometimes referred to as free as in beer. In practical terms, this means that you can download and use R free of charge.

    Another benefit, albeit slightly more indirect, is that anybody can access the source code, modify it, and improve it. As a result, many excellent programmers have contributed improvements and fixes to the R code. For this reason, R is very stable and reliable.

    technicalstuff.eps Any freedom also has associated obligations. In the case of R, these obligations are described in the conditions of the license under which it is released: GNU General Public License (GPL), Version 2. The full text of the license is available at www.r-project.org/COPYING. It’s important to stress that the GPL does not pertain to your usage of R. There are no obligations for using the software — the obligations just apply to redistribution. In short, if you change or redistribute the R source code, you have to make those changes available for anybody else to use.

    It runs anywhere

    The R Development Core Team has put a lot of effort into making R available for different types of hardware and software. This means that R is available for Windows, Unix systems (such as Linux), and the Mac.

    It supports extensions

    R itself is a powerful language that performs a wide variety of functions, such as data manipulation, statistical modeling, and graphics. One really big advantage of R, however, is its extensibility. Developers can easily write their own software and distribute it in the form of add-on packages. Because of the relative ease of creating these packages, literally thousands of them exist. In fact, many new (and not-so-new) statistical methods are published with an R package attached.

    It provides an engaged community

    The R user base keeps growing. Many people who use R eventually start helping new users and advocating the use of R in their workplaces and professional circles. Sometimes they also become active on the R mailing lists (www.r-project.org/mail.html) or question-and-answer (Q&A) websites such as Stack Overflow, a programming Q&A website (www.stackoverflow.com/questions/tagged/r) and CrossValidated, a statistics Q&A website (http://stats.stackexchange.com/questions/tagged/r). In addition to these mailing lists and Q&A websites, R users participate in social networks such as Twitter (www.twitter.com/search/rstats) and regional R conferences. (See Chapter 11 for more information on R communities.)

    It connects with other languages

    As more and more people moved to R for their analyses, they started trying to combine R with their previous workflows, which led to a whole set of packages for linking R to file systems, databases, and other applications. Many of these packages have since been incorporated into the base installation of R.

    For example, the R package foreign (http://cran.r-project.org/web/packages/foreign/index.html) is part of the standard R distribution and enables you to read data from the statistical packages SPSS, SAS, Stata, and others (see Chapter 12).

    Several add-on packages exist to connect R to database systems, such as the RODBC package, to read from databases using the Open Database Connectivity protocol (ODBC) (http://cran.r-project.org/web/packages/RODBC/index.html), and the ROracle package, to read Oracle data bases (http://cran.r-project.org/web/packages/ROracle/index.html).

    technicalstuff.eps Initially, most of R was based on Fortran and C. Code from these two languages easily could be called from within R. As the community grew, C++, Java, Python, and other popular programming languages got more and more connected with R.

    Because many statisticians also worked with commercial programs, the R Development Core Team (and others) wrote tools to read data from those programs, including SAS Institute’s SAS and IBM’s SPSS. By now, many of the big commercial packages have add-ons to connect with R. Notably, SPSS has incorporated a link to R for its users, and SAS has numerous protocols that show you how to move data and graphics between the two packages.

    Looking At Some of the Unique Features of R

    R is more than just a domain-specific programming language aimed at statisticians. It has some unique features that make it very powerful, including the notion of vectors, which means that you can make calculations on many values at the same time.

    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.

    We’ll illustrate with some real R code. First, we’ll assign the values 1:5 to a vector that we’ll call x:

    > x <- 1:5

    > x

    [1] 1 2 3 4 5

    Next, we’ll add the value 2 to each element in the vector x and print the result:

    > 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.

    This feature of R is extremely powerful because it lets you perform many operations in a single step. In programming languages that aren’t vectorized, you’d have to program a loop to achieve the same result.

    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 processing 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, graphic 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.

    technicalstuff.eps 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 in 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 compiled code runs.

    warning_bomb.eps 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 paradigm of functional programming. 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

    arrow Looking at your R editing options

    arrow Starting R

    arrow Writing your first R script

    arrow Finding your way around the R workspace

    In order to start working in R, you need to use an editing tool. Which editing tool you use depends to some extent on your operating system, because R doesn’t provide a single graphical editor for all operating systems. The basic R install gives you the following:

    check.png Windows: A basic editor called RGui.

    check.png Mac OS X: A basic R editor called R.app.

    check.png Linux: There is no specific R editor on Linux, but you can use any editor (like Vim or Emacs) to edit your R code.

    At a practical level, this difference between operating systems doesn’t matter very much because 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 an R code editor, so in this chapter we briefly illustrate how to use R with the Windows RGui. Our advice also works on R.app. And if you work in Linux, you can simply type the code into your preferred editor.

    Fortunately, there is an alternative called RStudio, third-party software that provides a consistent user interface regardless of operating system. In addition to demonstrating how to work with the Windows RGui, we also illustrate how to use RStudio.

    After you’ve opened a console, we get you exercising your R muscles and writing some scripts. You do some calculations, create some numeric and text variables, get to 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 editor, RGui (short for R graphical user interface). Since the standard, basic R editors are so, well, basic, we also introduce you to RStudio. RStudio offers a richer editing environment than RGui and makes some common tasks easier and more fun.

    Exploring RGui

    As part of the process of downloading and installing R, you get the standard graphical user interface (GUI), called RGui. RGui gives you some tools to manage your R environment — most important, a console window. The console is where you type instructions, or scripts, and generally get R to do useful things for you.

    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. Here are a few other options:

    check.png Eclipse StatET (www.walware.de/goto/statet): Eclipse, another powerful integrated development environment, has a R add-in called StatET. If you’ve done software development on large projects, you may find Eclipse useful. Eclipse requires you to install Java on your computer.

    check.png Emacs Speaks Statistics (http://ess.r-project.org): Emacs, a powerful text and code editor, is widely used in the Linux world and also is available for Windows. It has a statistics add-in called Emacs Speaks Statistics (ESS), which is famous for having keyboard shortcuts for just about everything you could possibly do and for its very loyal fan base. If you’re a programmer coming from the Linux world, this editor may be a good choice for you.

    check.png Tinn-R (www.sciviews.org/Tinn-R): This editor, developed specifically for working with R, is available only for Windows. It has some nice features for setting up collections of R scripts in projects. Tinn-R is easier to install and use than either Eclipse or Emacs, but it isn’t as fully featured.

    Seeing the naked R console

    The standard installation process creates useful menu shortcuts (although this may not be true if you use Linux, because there is no standard RGui editor for Linux). In the menu system, look for a folder called R, and then find an icon called R followed by a version number (for example, R 2.13.2, as shown in Figure 2-1).

    When you open RGui for the first time, you see the R Console screen (shown in Figure 2-2), which lists some basic information such as your version of R and the licensing conditions.

    Figure 2-1: The shortcut icon for RGui is labeled with R followed by the version number.

    9781119962847-fg0201.eps

    Figure 2-2: A brand-new session in RGui.

    9781119962847-fg0202.eps

    Below all this information is the R prompt, denoted by a > symbol. The prompt indicates where you type your commands to R; you see a blinking cursor to the right of the prompt.

    We explore the R console in more depth in Discovering the Workspace, later in this chapter.

    Issuing a simple command

    Use the console to issue a very simple command to R. Type the following to calculate the sum of some numbers:

    > 24+7+11

    R responds immediately to your command, calculates the total, and displays it in the console:

    > 24+7+11

    [1] 42

    The answer is 42. R gives you one other piece of information: The [1] preceding 42 indicates that the value 42 is the first element in your answer. It is, in fact, the only element in your answer! One of the clever things about R is that it can deal with calculating many values at the same time, which is called vector operations. We talk about vectors later in this chapter — for now, all you need to know is that R can handle more than one value at a time.

    Closing the console

    To quit your R session, type the following code in the console, after the command prompt (>):

    > q()

    R asks you a question to make sure that you meant to quit, as shown in Figure 2-3. Click No, because you have nothing to save. This action closes your R session (as well as RGui, if you’ve been using RGui as your code editor).

    Figure 2-3: R asks you a simple question.

    9781119962847-fg0203.eps

    Dressing up with RStudio

    RStudio is a code editor and development environment with some very nice features that make code development in R easy and fun:

    check.png Code highlighting that gives different colors to keywords and variables, making it easier to read

    check.png Automatic bracket matching

    check.png Code completion, so you don’t have to type out all commands in full

    check.png Easy access to R Help, with some nice features for exploring functions and parameters of functions

    check.png Easy exploration of variables and values

    Because RStudio is available free of charge for Linux, Windows, and Apple iOS devices, we think it’s a good option to use with R. In fact, we like RStudio so much that we use it to illustrate the examples in this book. Throughout the book, you find some tips and tricks on how things can be done in RStudio. If you decide to use a different code editor, you can still use all the code examples and you’ll get identical results.

    To open RStudio, click the RStudio icon in your menu system or on your desktop, as shown in Figure 2-4. (You can find installation instructions in this book’s appendix.)

    Figure 2-4: Opening RStudio.

    9781119962847-fg0204.eps

    Once RStudio started, choose File⇒New⇒R Script.

    Your screen should look like Figure 2-5. You have four work areas:

    check.png Source: The top-left corner of the screen contains a text editor that lets you work with source script files. Here, you can enter multiple lines of code, save your script file to disk, and perform other tasks on your script. This code editor works a bit like every other text editor you’ve ever seen, but it’s smart. It recognizes and highlights various elements of your code, for example (using different colors for different elements), and it also helps you find matching brackets in your scripts.

    check.png Console: In the bottom-left corner, you find the console. The console in RStudio is identical to the console in RGui (refer to Seeing the naked R console, earlier in this chapter). This is where you do all the interactive work with R.

    check.png Workspace and history: The top-right corner is a handy overview of your workspace, where you can inspect the variables you created in your session, as well as their values. (We discuss the workspace in more detail later in this chapter.) This is also the area where you can see a history of the commands you’ve issued in R.

    check.png Files, plots, package, and help: In the bottom-right corner, you have access to several tools:

    Files: This is where you can browse the folders and files on your computer.

    Plots: This is where R displays your plots (charts or graphs). We discuss plots in Part V.

    Packages: This is where you can view a list of all the installed packages. A package is self-contained set of code that adds functionality to R, similar to the way that an add-in adds functionality to Microsoft Excel.

    Help: This is where you can browse the built-in Help system of R.

    Figure 2-5: RStudio’s four work areas.

    9781119962847-fg0205.eps

    Starting Your First R Session

    If you’re anything like the two of us, you’re probably just itching to get hold of some real code. In this section, you get to do exactly that. Get ready to get your hands dirty!

    Saying hello to the world

    Programming books typically start with a very simple program. Often, the objective of this first program is to create the message Hello world! In R, this program consists of one line of code.

    Start a new R session, type the following in your console, and press Enter:

    > print(Hello world!)

    R responds immediately with this output:

    [1] Hello world!

    Congratulations! You’ve just completed your first R script.

    remember.eps As we explain in the introduction to this book, we collapse these two things into a single block of code, like this:

    > print(Hello world!)

    [1] Hello world!

    Doing simple math

    Type the following in your console to calculate the sum of five numbers:

    > 1+2+3+4+5

    [1] 15

    The answer is 15, which you can easily verify for yourself. You may think that there’s an easier way to calculate this value, though — and you’d be right. We explain how in the following section.

    Using vectors

    A vector is the simplest type of data structure in R. The R manual defines a vector as a single entity consisting of a collection of things. A collection of numbers, for example, is a numeric vector — the first five integer numbers form a numeric vector of length 5.

    To construct a vector, type the following in the console:

    > c(1,2,3,4,5)

    [1] 1 2 3 4 5

    In constructing your vector, you have successfully used a function in R. In programming language, a function is a piece of code that takes some inputs and does something specific with them. In constructing a vector, you tell the c() function to construct a vector with the first five integers. The entries inside the parentheses are referred to as arguments.

    You also can construct a vector by using operators. An operator is a symbol you stick between two values to make a calculation. The symbols +, -, *, and / are all operators, and they have the same meaning they do in mathematics. Thus, 1+2 in R returns the value 3, just as you’d expect.

    One very handy operator is called sequence, and it looks like a colon (:). Type the following in your console:

    > 1:5

    [1] 1 2 3 4 5

    That’s more like it. With three keystrokes, you’ve generated a vector with the values 1 through 5. Type the following in your console to calculate the sum of this vector:

    > sum(1:5)

    [1] 15

    Storing and calculating values

    Using R as a calculator is very interesting but perhaps not all that useful. A much more useful capability is storing values and then doing calculations on these stored values.

    Try the following:

    > x <- 1:5

    > x

    [1] 1 2 3 4 5

    In these two lines of code, you first assign the sequence 1:5 to a variable called x. Then you ask R to print the value of x by typing x in the console and pressing Enter.

    remember.eps In R, the assignment operator is <-, which you type in the console by using two keystrokes: the less-than symbol (<) followed by a hyphen (-). The combination of these two symbols represents assignment.

    In addition to retrieving the value of a variable, you can do calculations on that value. Create a second variable called y, and assign it the value 10. Then add the values of x and y, as follows:

    > y <- 10

    > x + y

    [1] 11 12 13 14 15

    The values of the two variables themselves don’t change unless you assign a new value. You can check this by typing the following:

    > x

    [1] 1 2 3 4 5

    > y

    [1] 10

    Now create a new variable z, assign it the value of x+y, and print its value:

    > z <- x + y

    > z

    [1] 11 12 13 14 15

    Variables also can take on text values. You can assign the value Hello to a variable called h, for example, by presenting the text to R inside quotation marks, like this:

    > h <- Hello

    > h

    [1] Hello

    remember.eps You must present text or character values to R inside quotation marks — either single or double. R accepts both. So both h <- Hello and h <- ‘Hello’ are examples of valid R syntax.

    In Using vectors, earlier in this chapter, you use the c() function to combine numeric values into vectors. This technique also works for text. Try it:

    > hw <- c(Hello, world!)

    > hw

    [1] Hello world!

    You can use the paste() function to concatenate multiple text elements. By default, paste() puts a space between the different elements, like this:

    > paste(Hello, world!)

    [1] Hello world!

    Talking back to the user

    You can write R scripts that have some interaction with a user. To ask the user questions, you can use the readline() function. In the following code snippet, you read a value from the keyboard and assign it to the variable yourname:

    > h <- Hello

    > yourname <- readline(What is your name?)

    What is your name?Andrie

    > paste(h, yourname)

    [1] Hello Andrie

    This code seems to be a bit cumbersome, however. Clearly, it would be much better to send these three lines of code simultaneously to R and get them evaluated in one go. In the next section, we show you how.

    Sourcing a Script

    Until now, you’ve worked directly in the R console and issued individual commands in an interactive style of coding. In other words, you issue a command, R responds, you issue the next command, R responds, and so on.

    In this section, you kick it up a notch and tell R to perform several commands one after the other without waiting for additional instructions. Because the R function to run an entire script is source(), R users refer to this process as sourcing a script.

    To prepare your script to be sourced, you first write the entire script in an editor window. In RStudio, for example, the editor window is in the top-left corner of the screen (refer to Figure 2-5). Whenever you press Enter in the editor window, the cursor moves to the next line, as in any text editor.

    Type the following lines of code in the editor window. (Remember that in RStudio the source editor is in the top-left corner, by default.) Notice that the last line contains a small addition to the code you saw earlier: the print() function.

    h <- Hello

    yourname <- readline(What is your name?)

    print(paste(h, yourname))

    remember.eps Remember to type the print() function as part of your script. Sourced scripts behave differently from interactive code in printing results. In interactive mode, a result is printed even without a print() function. But when you source a script, output is printed only if you have an explicit print() function.

    You can type multiple lines of code into the source editor without having each line evaluated by R. Then, when you’re ready, you can send the instructions to R — in other words, source the script.

    When you use RGui or RStudio, you can do this in one of three ways:

    check.png Send an individual line of code from the editor to the console. Click the line of code you want to run, and then press Ctrl+R in RGui. In RStudio, you can press Ctrl+Enter or click the Run button.

    check.png Send a block of highlighted code to the console. Select the block of code you want to run, and then press Ctrl+R (in RGui) or Ctrl+Enter (in RStudio).

    check.png Send the entire script to the console (which is called sourcing a script). In RGui, click anywhere in your script window, and then choose Edit⇒Run all. In RStudio, click anywhere in the source editor, and press Ctrl+Shift+Enter or click the Source button.

    remember.eps These keyboard shortcuts are defined only in RStudio. If you use a different source editor, you may not have the same options.

    Now you can send the entire script to the R console. To do this, click the Source button in the top-right corner of the editor window or choose Edit⇒Source. The script starts, reaches the point where it asks for input, and then waits for you to enter your name in the console window. Your screen should now look like Figure 2-6. Notice that the Workspace window now lists the two objects you created: h and yourname.

    Figure 2-6: Sending a script to the console in RStudio.

    9781119962847-fg0206.eps

    technicalstuff.eps When you click the Source button, source(‘~/.active-rstudio- document’) appears in the console. What RStudio actually does here is save your script in a temporary file and then use the R function source() to call that script in the console. Remember this function; you’ll meet it again.

    Finding help on functions

    We discuss R’s built-in help system in Chapter 11, but for now, to get help on any function, type ? in the console. To get help with the save() function, for example, type the following:

    > ?paste

    This code opens a Help window. In RStudio, this Help window is in the bottom-right corner of your screen by default. In other editors, the Help window sometimes appears as a local web page in your default web browser.

    You also can type help, but remember to use parentheses around your search term, like so:

    > help(paste)

    Navigating the Workspace

    So far in this chapter, you’ve created several variables. These form part of what R calls the workspace, which we explore in this section. The workspace refers to all the variables and functions (collectively called objects) that you create during the session, as well as any packages that are loaded.

    Often, you want to remind yourself of all the variables you’ve created in the workspace. To do this, use the ls() function to list the objects in the workspace. In the console, type the following:

    > ls()

    [1] h        hw       x        y        yourname z

    R tells you the names of all the variables that you created.

    tip.eps One very nice feature of RStudio lets you examine the contents of the workspace at any time without typing any R commands. By default, the top-right window in RStudio has two tabs: Workspace and History. Click the Workspace tab to see the variables in your workspace, as well as their values. (refer to Figure 2-5).

    Manipulating the content of the workspace

    If you decide that you don’t need some variables anymore, you can remove them. Suppose that the object z is simply the sum of two other variables and no longer needed. To remove it permanently, use the rm() function and then use the ls() function to display the contents of the workspace, as follows:

    > rm(z)

    > ls()

    [1] h        hw       x        y        yourname

    Notice that the object z is no longer there.

    Saving your work

    You have several options for saving your work:

    check.png You can save individual variables with the save() function.

    check.png You can save the entire workspace with the save.image() function.

    check.png You can save your R script file, using the appropriate save menu command in your code editor.

    Suppose you want to save the value of yourname. To do that, follow these steps:

    1. Find out which working directory R will use to save your file by typing the following:

    > getwd()

    [1] c:/users/andrie

    The default working directory should be your user folder. The exact name and path of this folder depend on your operating system. (In Chapter 12, you get

    Enjoying the preview?
    Page 1 of 1