Chapter 1. RefresheR

Before we dive into the (other) fun stuff (sampling multi-dimensional probability distributions, using convex optimization to fit data models, and so on), it would be helpful if we review those aspects of R that all subsequent chapters will assume knowledge of.

If you fancy yourself as an R guru, you should still, at least, skim through this chapter, because you'll almost certainly find the idioms, packages, and style introduced here to be beneficial in following along with the rest of the material.

If you don't care much about R (yet), and are just in this for the statistics, you can heave a heavy sigh of relief that, for the most part, you can run the code given in this book in the interactive R interpreter with very little modification, and just follow along with the ideas. However, it is my belief (read: delusion) that by the end of this book, you'll cultivate a newfound appreciation of R alongside a robust understanding of methods in data analysis.

Fire up your R interpreter, and let's get started!

Navigating the basics

In the interactive R interpreter, any line starting with a > character denotes R asking for input (If you see a + prompt, it means that you didn't finish typing a statement at the prompt and R is asking you to provide the rest of the expression.). Striking the return key will send your input to R to be evaluated. R's response is then spit back at you in the line immediately following your input, after which R asks for more input. This is called a REPL (Read-Evaluate-Print-Loop). It is also possible for R to read a batch of commands saved in a file (unsurprisingly called batch mode), but we'll be using the interactive mode for most of the book.

As you might imagine, R supports all the familiar mathematical operators as most other languages:

Arithmetic and assignment

Check out the following example:

  > 2 + 2
  [1] 4

  > 9 / 3
  [1] 3

  > 5 %% 2    # modulus operator (remainder of 5 divided by 2)
  [1] 1

Anything that occurs after the octothorpe or pound sign, #, (or hash-tag for you young'uns), is ignored by the R interpreter. This is useful for documenting the code in natural language. These are called comments.

In a multi-operation arithmetic expression, R will follow the standard order of operations from math. In order to override this natural order, you have to use parentheses flanking the sub-expression that you'd like to be performed first.

  > 3 + 2 - 10 ^ 2        # ^ is the exponent operator
  [1] -95
  > 3 + (2 - 10) ^ 2
  [1] 67

In practice, almost all compound expressions are split up with intermediate values assigned to variables which, when used in future expressions, are just like substituting the variable with the value that was assigned to it. The (primary) assignment operator is <-.

  > # assignments follow the form VARIABLE <- VALUE
  > var <- 10
  > var
  [1] 10
  > var ^ 2
  [1] 100
  > VAR / 2             # variable names are case-sensitive 
  Error: object 'VAR' not found

Notice that the first and second lines in the preceding code snippet didn't have an output to be displayed, so R just immediately asked for more input. This is because assignments don't have a return value. Their only job is to give a value to a variable, or to change the existing value of a variable. Generally, operations and functions on variables in R don't change the value of the variable. Instead, they return the result of the operation. If you want to change a variable to the result of an operation using that variable, you have to reassign that variable as follows:

  > var               # var is 10
  [1] 10
  > var ^ 2
  [1] 100
  > var               # var is still 10
  [1] 10
  > var <- var ^ 2    # no return value
  > var               # var is now 100
  [1] 100

Be aware that variable names may contain numbers, underscores, and periods; this is something that trips up a lot of people who are familiar with other programming languages that disallow using periods in variable names. The only further restrictions on variable names are that it must start with a letter (or a period and then a letter), and that it must not be one of the reserved words in R such as TRUE, Inf, and so on.

Although the arithmetic operators that we've seen thus far are functions in their own right, most functions in R take the form: function_name (value(s) supplied to the function). The values supplied to the function are called arguments of that function.

  > cos(3.14159)      # cosine function
  [1] -1
  > cos(pi)           # pi is a constant that R provides
  [1] -1
  > acos(-1)          # arccosine function
  [1] 2.141593
  > acos(cos(pi)) + 10 
  [1] 13.14159
  > # functions can be used as arguments to other functions

(If you paid attention in math class, you'll know that the cosine of π is -1, and that arccosine is the inverse function of cosine.)

There are hundreds of such useful functions defined in base R, only a handful of which we will see in this book. Two sections from now, we will be building our very own functions.

Before we move on from arithmetic, it will serve us well to visit some of the odd values that may result from certain operations:

  > 1 / 0
  [1] Inf
  >  0 / 0
  [1] NaN

It is common during practical usage of R to accidentally divide by zero. As you can see, this undefined operation yields an infinite value in R. Dividing zero by zero yields the value NaN, which stands for Not a Number.

Logicals and characters

So far, we've only been dealing with numerics, but there are other atomic data types in R. To wit:

  > foo <- TRUE        # foo is of the logical data type
  > class(foo)         # class() tells us the type
  [1] "logical"
  > bar <- "hi!"       # bar is of the character data type
  > class(bar)
  [1] "character"

The logical data type (also called Booleans) can hold the values TRUE or FALSE or, equivalently, T or F. The familiar operators from Boolean algebra are defined for these types:

  > foo
  [1] TRUE
  > foo && TRUE                 # boolean and
  [1] TRUE
  > foo && FALSE
  [1] FALSE
  > foo || FALSE                # boolean or
  [1] TRUE
  > !foo                        # negation operator
  [1] FALSE

In a Boolean expression with a logical value and a number, any number that is not 0 is interpreted as TRUE.

  > foo && 1
  [1] TRUE
  > foo && 2
  [1] TRUE
  > foo && 0
  [1] FALSE

Additionally, there are functions and operators that return logical values such as:

  > 4 < 2           # less than operator
  [1] FALSE
  > 4 >= 4          # greater than or equal to
  [1] TRUE
  > 3 == 3          # equality operator
  [1] TRUE
  > 3 != 2          # inequality operator
  [1] TRUE

Just as there are functions in R that are only defined for work on the numeric and logical data type, there are other functions that are designed to work only with the character data type, also known as strings:

  > lang.domain <- "statistics"
  > lang.domain <- toupper(lang.domain)
  > print(lang.domain)
  [1] "STATISTICS"
  > # retrieves substring from first character to fourth character
  > substr(lang.domain, 1, 4)          
  [1] "STAT"
  > gsub("I", "1", lang.domain)  # substitutes every "I" for "1"
  [1] "STAT1ST1CS"
  # combines character strings
  > paste("R does", lang.domain, "!!!")
  [1] "R does STATISTICS !!!"

Flow of control

The last topic in this section will be flow of control constructs.

The most basic flow of control construct is the if statement. The argument to an if statement (what goes between the parentheses), is an expression that returns a logical value. The block of code following the if statement gets executed only if the expression yields TRUE. For example:

  > if(2 + 2 == 4)
  +     print("very good")
  [1] "very good"
  > if(2 + 2 == 5)
  +     print("all hail to the thief")
  >

It is possible to execute more than one statement if an if condition is triggered; you just have to use curly brackets ({}) to contain the statements.

  > if((4/2==2) && (2*2==4)){
  +     print("four divided by two is two...")
  +     print("and two times two is four")
  + }
  [1] "four divided by two is two..."
  [1] "and two times two is four"
  >

It is also possible to specify a block of code that will get executed if the if conditional is FALSE.

  > closing.time <- TRUE
  > if(closing.time){
  +     print("you don't have to go home")
  +     print("but you can't stay here")
  + } else{
  +     print("you can stay here!")
  + }
  [1] "you don't have to go home"
  [1] "but you can't stay here"
  > if(!closing.time){
  +     print("you don't have to go home")
  +     print("but you can't stay here")
  + } else{
  +     print("you can stay here!")
  + }
  [1] "you can stay here!"
  >

There are other flow of control constructs (like while and for), but we won't directly be using them much in this text.

..................Content has been hidden....................

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