Category Archives: R

Starting out with R

This will be a simple “getting started” post on the R environment and language. R is a statistical language which is also heavily used in data mining and machine learning.

For this post I’m using R version 3.1.2 and the free version of R Studio 0.98.1087. Assuming we have R installed and then R Studio, let’s get started…

Demos

Let’s start by looking at the demo command. Typing demo() within the console of R Studio will list the available demos within the packages installed. So if we install further packages which contain demos, these will be available when you invoke the demo command.

So demo will list the demos within the packages, we can invoke

demo(package='graphics')

to get the graphics package demos and to run a demo simply type

demo(image)

for example to run the image demo.

To find a list of all demos within all packages we can invoke

demo(package = .packages(all.available=TRUE))

R scripts

By default we can execute commands in the R console, but ofcourse we might wish to put a whole bunch of commands together into a script file, so within R Studio we can use Ctrl+Shift+N to create a new script file and then obviously enter the script. We can then run the script using Ctrl+Shift+S (or Code | Source).

Help

The help function can be invoked from the console or R Studio to view help on a function/command, (this will load an HTML page, in the case of R Studio this is displayed within the IDE)

# display the help documentation
help() 

# display the help documentation for the vector type
help(vector)

We can also use the shortcut ? to invoke the help

?vector

Vignette

Vignettes are documents (in PDF format) which are available as help for packages.

vignette()

the above will, like demo() list all vignettes in the packages.

Again we can list all vignettes across all packages using

vignette(package = .packages(all.available = TRUE))

and you guessed it we can equally run the following to list the vignettes within a specific package

vignette(package = 'grid')

Finally to invoke a vignette we simple execute

vignette('grid')

to execute a specific vignette on a specific package we can equally write

vignette('moveline', package='grid')

Double or single quote symbols?

We can use either double or single quotes, interchangeably within R but obviously if we want to embed a single or double quote within a string, we can use the other type of quote, for example

s <- 'This is a "double quote" string'

As per C# we can equally use the backslash to escape the quote

s <- "This is a \"double quote\" string"

Comments

Before we start writing code, let’s look at how to write comments within R. The # is used to prefix a comment, for example

# this is a comment

Variables

Like most programming languages, we want ways to store data, like F# we use the <- operator to assign values to a variable, i.e. [code language="R"] x <- 123 [/code] so obviously in the above x is the variable name and is now assigned the value 123. We can also use the assign variables using the assign function, for example [code language="R"] assign("x", 123) [/code] notice the variable name is written as a string. We can also use -> and = to assign variables

123 -> x

x = 123

however the equals operator is rarely used from what I can tell, partly because it can have different meanings in different situations.

Variable names in R can be made up of letters, numbers, dot and underline characters. Variables must start with a letter or dot (note: a dot cannot be followed by a number as a variable name). Ofcourse like other languages, R keywords cannot be used for variable names.

If you’re interested in R style guidelines which include guidelines for variable naming styles, checkout Google’s R Style Guide.

Once we create a variable it’s added to the global environment, but we might also wish to remove a variable from the global environment, to do this execute

rm("x")

# or use

remove("x")

where x is the variable name we want to remove.

Finally we can view the values within a variable using

print(x)

# or simply typing the variable name and pressing enter

x

either of the above will output the value stored within the variable x.

Scope/Environments

If we simply assign variable (as previously discussed) the variable will be added to the environment/global scope, however we can also create alternate scope known as a custom environment.

We create a new environment using the followings

e <- new.env()

where e is a variable of type Environment. We can then assign variables to this environment using

assign("x", 123, e)

# or 

e[["x"]] <- 123

#or

e$x <- 123

in all of the above our new environment variable e is used and the variable x is created within the environment scope and assigned the value 123.

Note: in the last example with the $ quotes are optional around the variable name

To output the variable from the new Environment we can simply type

e$x

# or

get("x", e)

# or

e[["x"]]

More on Environments

We can also get a variable from the global environment using get, such as

get("x", globalenv()) 

and if we want to find the parent environment to a custom environment we can use

parent.env(e)

Operators

We’ve already covered the assignment operator <-, let's look at some of the other available operators. Arithmetic Operators

a <- 10  # assign a the value 10
b <- 5   # assign b the value 5

a + b    # addition operator
a - b    # subtraction operator
a * b    # multiplication operator
a / b    # division operator
a ^ b    # exponent operator
a ** b   # alternate exponent operator
a %% b   # modulus operator
a %/% b  # integer division

Logical operators

a < b    # less than operator
a > b    # greater than operator
a <= b   # less than or equal operator
a >= b   # less than or equal operator
a == b   # equality operator
a != b   # not equal operator
!a       # logical NOT operator
a | y    # logical OR operator
a & y    # logical AND operator

We can also use the function isTRUE(a) to test if a variable is TRUE.

This covers the real basics of getting up and running with R. I hope to create further posts covering more aspects of R real soon.