Link to home

Creating objects and assigning values

Objects are assigned values using <- , an arrow formed out of < and -. (An equal sign, =, can also be used.) For example, the following command assigns the value 5 to the object x.

x <- 5

After this assignment, the object x ‘contains’ the value 5. Another assignment to the same object will change the content.

x <- 107

You can check the content of an object by simply entering the name of the object on an interactive command line. Try that throughout these examples to see what the results are of the different operations and functions illustrated.

R is case sensitive – that is, it treats data15 and Data15 as completely different objects.

This can be a hassle if you write code in a separate file using a word processor that automatically changes case at the beginning of sentences, etc. In Word, you can change 'autocorrect' options in the Menu under Tools, Autocorrect options,… Using an editor like Notepad is a better option when simply writing R code.

An editor is also supplied within the R window through the menu under File, New Script. The simplest way to develop commands in R and then implement them may be to write the code in the R Editor window and then submit the code through the Edit, Run… options.

Free editors designed for R are available that highlight code syntax as in this document and may make working with R easier in other ways as well. Sciviews-R is a Graphical User Interface (GUI) for R for Windows. For Linux and BSD, two Unix-like operating systems, RKWard is a transparent front end for R based on KDE, a desktop environment for Unix systems, that strives to make R easier to use. This program was used while creating this HTML document to highlight the R-code. Other programs such as RCommander, and Emacs/ESS are available as well. While the use of programs such as these will not be covered in this document, you may find them helpful to you as you work through these documents or in the future if you choose to use R in your research.

Comments should be added to programs to clarify steps, define variables, etc., so that anyone reading a program now or in the future can more easily make sense of what the program does. The part of a line that begins with # does not get treated as code, rather as a comment. For example, after the following two lines of code, x still has the value 5.

x <- 5
# x <- 6

Try creating some other objects and assigning values to them. Naming of objects is very flexible, though names must begin with a letter and problems can be avoided if you don’t name objects with the same name as an existing function in R. Picking a name that has a clear meaning is particularly helpful for programs that contain many lines. If the R workspace is saved (more about this below), it is important to take note of what objects have been created, so that old objects are not accidentally used incorrectly in future sessions.

Some names have already been assigned in R, like mean, function, etc.

For example, ‘pi’ is already defined (π = 3.1416).

Try entering the following command line, pi.

An object can be assigned a set of numbers, as for example:

x12 <- c(10,6,8)

Here c combines the numbers 10, 6, and 8 into a vector.

Operations can then be performed on the whole set of numbers. For example, for the object x12 created above, check the results of the following:

x12 * 10x12/3

The numbers in x can be indexed individually, for example:

x12[2] # gives the second entry in x12
x12[1] # gives the first entry in x12

Or the numbers in x can be indexed in subsets. Consider the results of the following two commands:

x12[1:3] # gives the first through third entries in x12
x12[c(1,3)] # gives the first and third entries in x12

What is the difference between the these two commands?

Logical variables can be used in evaluation of the entries in an object. Logical variables take on the values TRUE or FALSE. Try:

x12 < 3

Logical variables can also be assigned to objects, for example:

y <- x12 < 3

Here the object y is assigned the vector of TRUE and FALSE values corresponding to whether the entries in x12 are less than three or not.




R includes many functions, especially for statistical summaries and analyses.

Try these functions on x12 and other objects that you create.

mean(x12) # calculates the mean of the entries in x12
var(x12) # calculates the variance of the entries in x12
cumsum(x12) # check what this function is doing…
sum(y) # logical values can be 'coerced' to numeric,
where FALSE = 0 and TRUE = 1
# - here the sum is a count of 'TRUE's

The last example can be modified to calculate proportions or percentages. For example, if y is a set of logical variables indicating whether plants are heavily infected,


gives the proportion of plants heavily infected. sum(y) gives the number of plants infected and length(y) gives the total number of plants.

The help function gives information about how a function works. For example, help(mean).

The example function gives examples of applications of a function, example(length).

Throughout these examples, try using the help and example commands to better understand what programs are doing.

# gives a list of all the objects in your working directory
# removes the objects indicated.
# For example the object x is removed by

You might want to clear out old objects that you don’t need anymore to avoid confusion.


Next: Graphics