The Basics Learning R starts with learning the basics of the programming language. The following sections should provide you with a basic understanding to allow you to hit the ground running.

The Console Workspace As noted in the "Learning the Interface" page, the console workspace is where you enter and run code. When you first launch R, you will be met with a prompt ( > |) where you can begin coding.

Simple Expressions When you type something at the prompt and hit return, R will immediately evaluate, compute, and print the result to that entry. To start, try typing a very simple addition expression at the prompt and hit return. In the example below, "1 + 2" was entered at the prompt and the result "3" was returned. Note that the "[1]" next to the result identified the line of the result (results requiring multiple lines will have additional markers).

> 1 + 2 [1] 3

Now try a division expression ("/" is the division operator). In the example below, "6 / 3" was entered at the result of "2" was returned.

> 6 / 3 [1] 2

You can also enter character strings at the prompt and R will return the string back to you. The strings must be contained within quotes however. Try typing a string. In the example below "Two if by sea" was entered and teh same string was returned.

> "The Answer to Everything" [1] "The Answer to Everything"

A list of common operators used in simple expressions can be found here.

Variables You can define variables in the console workspace and recall them later. Try defining the variable "x" as 42 and the variable "y" as 58 as shown below.

> x = 42 > y = 58

You can recall a variable by simple typing the variable and hitting return. Try recalling the x variable as shown below.

> x [1] 42

The variables can now be used in expressions. Try using the variables in the expressions. The examples below show the now defined x and y values being used in a few expressions.

> x * 2 [1] 84

> x + y [1] 100

You can also define variables as character strings. Try defining the variable "Moon" as "One Small Step" as shown below. Remember, character strings have to be contained within quotes.

> Moon = "One Small Step" > Moon [1] "One Small Step"

Note that variable names are case sensitive. Therefore "Moon" and "moon" are different variables.

Logical Expressions You can also use R to define and evaluate Logical Expressions (TRUE/FALSE). Try entering a TRUE logical expression using the less than ( < ) operator. In the example below, "2 < 10" was entered and R returned a result of "TRUE".

> 2 < 10 [1] TRUE

When defining a logical expression with an equality you have to use a double equals sign. Try entering a FALSE expression with a double equals sign. In the example below, "1 + 1 == 3" was entered and R returned a result of "FALSE"

> 1 + 1 == 3 [1] FALSE

A list of common operators used in logical expressions can be found here.

Functions A large number Functions are built into R to streamline some of the most common tasks (these are analogous to the function available in Excel). Try using the "sqrt" function to find the square root of the "x" variable previously defined.

> sqrt( x ) [1] 6.480741

Another common function is the "sum" function. Try using the "sum" function to determine the sum of the all the numbers within the range 1-100. (You can define the range 1-100 using "1:100")

> sum( 1:100 ) [1] 5050

Vectors A vector is a one dimension array of data. Think of it as a single column in a typical spreadsheet. Multiple entries of data that all share a common format. A vector can be created in R with the combine ("c") function. The example below shows the creation of a vector containing the string 1, 2, 3.

> c(1 , 2 , 3) [1] 1 2 3

You can also create a vector of character strings. The example below shows a vector containing the months of the year.

You can create a vector that is a defined numerical range using the sequence ("seq") function. The example below creates a vector that is a range from 1 to 2, with 0.1 increments.

You can return a specific value from a vector by using it's numeric index surrounded by brackets. The example below calls out the 5th month of the year using the Months vector we defined previously.

> Months [ 5 ] [1] "May"

It is possible to pull multiple values using this same technique. Note that the numeric indexes must be combined first using the "c" function. In the example below, the first three months are called out from the Months vector.

> Months [ c( 1, 2, 3 ) ] [1] "Jan" "Feb" "Mar"

You can add a value to a vector using this numeric index technique as well. The example below adds a 13th month to the Months vector.

Operators can be applied to vectors as well. When applied, most operators will execute on each value independently. The example below defines a vector "Numbers" as a range from 1:10. It then multiplies this vector by 2.