Quick start

To install R, simply navigate to http://www.r-project.org and choose a mirror near you. Then, select whether you want R for Linux, Windows, or Mac. Finally, just follow the instructions from there, and you'll be up in no time. For additional FAQs, refer to http://cran.r-project.org/faqs.html.

Quick start

In addition to installing R, you will almost certainly want to install an integrated development environment (IDE). An IDE is a programming environment that offers features beyond what is found by using the terminal or a command-line environment. These features can include code editing/highlighting/completion/generation, code compiling, code debugging, a file manager, a package manager, and a graphical user interface (GUI). These features will make generating and managing your R code simpler. There are a plethora of options, but we have a slight preference for RStudio, which is shown in the previous screenshot. It is recommended that you install RStudio (http://www.rstudio.com) before working through the examples discussed in this book. As we move forward, note that all of the following R code will be available online on the authors' web page and GitHub.

The basics – assignment and arithmetic

R allows access to the central math operators through their standard character representations: exponentiation (^), multiplication (*), division (/), addition (+), and subtraction (-). As you will see in the next example, R respects the order of operations.

The carrot (>) symbol denotes lines of code being inputted to R, while lines without (>) denote the output from R. In some circumstances, R will number its output; we'll point that out as it arises. Finally, R does not read code following a pound sign (#), which allows users to write comments for themselves and others right in the code itself:

> 2^4-3
13
> 2^(4-3)
2

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Functions, arguments, and help

R has many built-in functions. A function is a programming construct that takes input, calls arguments, and turns them into output. Some functions only take one argument. An example of this is as follows:

> sqrt(16)
4

Other functions take several arguments. Generally, R can use the order in which you provide the arguments to understand the arguments respectively; however, it is a good practice to explicitly label your arguments, which are generally separated by commas. R does not read or care about spaces, but it is a good practice to include spaces between operators and after commas for better readability, as follows:

# Take the log of 100 with base 10
> log(100, 10)
2

# Though not necessary, it is best practice to label arguments
# This avoids confusion when functions take many arguments
> log(100, base=10)
2

To get help with a function, you can use the help function or type a question mark before a term. Using double question marks broadens the search, as shown in the following code:

> help(log)
> ?log
> ??log

Assignment is an important concept in R. We can assign values to an object, then treat that object as if it were the value it stores. An example should make this much more clear. Note the use of the left-facing arrow (<-) for assignment. Although you can assign with a right arrow or a single equals sign, only using the left arrow helps avoid confusion. An example of the assignment concept is shown as follows:

# Assign the value 3 to the object called 'my.variable'
> my.variable<- 3
# Work with the object
> my.variable * 2
[1] 6
# Create a new variable
> other.object<- my.variable + 7
> other.object * 2
[1] 20

R utilizes logical operators in addition to arithmetic operators. Logical operators are those that compare entities and return values of either TRUE or FALSE. Note that the double equals sign is used to ask a question, while the single equals sign is used for assignment (though the arrow should be strongly preferred for assignment to avoid confusion):

> 2==3
[1] FALSE
> 4 >= 4
[1] TRUE
"hello" != "HELLO"
[1] TRUE
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.47.25