Vries Andrie de

R For Dummies


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

operations with a small amount of code. As vectors are the smallest possible unit of data in R, you get to work with vectors extensively in later chapters.

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 this:

      > x <– 1:5

      > x

      [1] 1 2 3 4 5

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

      

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. It's good practice to always surround the <- with spaces. This makes your code much easier to read and understand.

      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 to either of them. 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"

      

You must enter 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. For consistency, we use double quotes throughout this book.

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

      > hw <– c("Hello", "world!")

      > hw

      [1] "Hello" "world!"

      You 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-4). Whenever you press Enter in the editor window, the cursor moves to the next line, as in any text editor.

      To create a new script in RStudio, begin by opening the editor window (choose File ⇒ New File ⇒ R script to open the editor window). Type the following lines of code in the editor window. 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 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 without needing to use a print() function. But when you source a script, output is by default 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:

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

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

      ✔ 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+S or click the Source button.

      

These keyboard shortcuts are defined only in RGui or RStudio. If you use a different source editor, you may have different 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-5. Notice that the Environment pane now lists the two objects you created: h and yourname.