# The R Environment:

The Console, the Command Line, and Variables

When you first launch R, you will see a window that looks something like the one in the image to the right. This is a command line interface rather than a menu-driven interface that you may be more accustomed to using . In a command line interface, you type commands that you want to execute and press return. For example, if you type the line `2+2`

and press the return key, R will give you the result `[1] 4`

.

In this mode, R can be used as a very simple calculator for addition, subtraction, multiplication, and division using the standard operators `+, -, *,`

and `/`

. This ability to enter commands is the fundamental building block for using the R program. In this tutorial, we will learn many other commands to calculate statistics for our data sets.

In order to use this console effectively, we also need to know how to store data in variables. In R, a variable is a name that is assigned a particular value. The variable names are then used in place of numbers to complete calculations.

Values can be assigned to variables using one of three operators: `<-, =, `

and `->`

. You can assign the number 5 to the variable `v1`

with any of the following commands.

- v1 <- 5
- v1 = 5
- 5 -> v1

In each case, you can confirm the assigned value by typing `v1`

and pressing return. R will give the following output:

` v1 <- 5`

> v1

[1] 5

You can then assign the value of 6 to the variable `v2`

using the command `v2 <- 6`

and issue a command such as `v1 * v2`

and get a result of 30.

There are two other important data formats that you need to be familiar with in order to use R: vectors and data frames. A vector is simply a list of numbers stored under a single name. A vector can be created using the `c`

command as follows: `list1 <- c(1, 2, 5, 7, 8, 10, 24)`

.

You can confirm the value of this variable by entering `list1`

on the command line and pressing return. You can examine a specific value in the vector by entering its numerical position inside of square brackets. Typing `list1[3]`

, for example, will give the result `5`

. (The R documentation for vectors has more detail at http://cran.r-project.org/doc/manuals/R-intro.html#Vectors-and-assignment.)

Many R commands work on vectors rather than single values. For example, you can quickly find the sum of this list by using the `sum()`

command; if you type `sum(list1)`

, R gives the result `57`

.

R has a third data structure that we will use most extensively throughout this tutorial called a data frame. A data frame offers us a table structure that is similar to what you might use in a spreadsheet. In the next section, we will examine how to import data into a data frame and then use that data as the basis for exploring many other R commands.

<<-- Previous: Getting Started With R

Next: Preparing and Importing Data -->>