6  R objects

When we have finished this chapter, we should be able to:

Learning objectives
  • Understand the concept of objects in R.
  • Know the types of assignment operators in R.
  • Set legal names to objects.
  • Work with assignment operators.

 

6.1 What are the objects in R

R works with objects (it is an object-oriented programming language). All the things that we manipulate or encounter in R such as numbers, data structures, functions, the results from a function (e.g., plots) are types of objects. Objects come within the R packages or they are user-created. The latter have names that are assigned by the user. R stores objects in the global environment.

Objects in R usually have many properties associated with them, called attributes. These properties explain what an object represents and how it should be interpreted by R. Two of the most important attributes of an R object are the class and the dimension of the object. Attributes of an object can be accessed using the attributes() function. Not all R objects contain attributes, in which case the attributes() function returns NULL.

For example, the attributes of the famous iris data set is a data.frame that contains 150 rows and 5 columns:

class(iris); dim(iris)   # Note: R commands can be separated by a semicolon
[1] "data.frame"
[1] 150   5
$names
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     

$class
[1] "data.frame"

$row.names
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
 [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
 [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
 [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
 [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
 [91]  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
[109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
[127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150
Avoid to separate R commands with a semicolon

R commands are usually separated by a new line but they can also be separated by a semicolon (;). However, this is not the optimal practice and should be avoided wherever possible.

6.2 Named storage of objects

6.2.1 Assignment operator (<-)

In R we can store things in objects using the leftward assignment operator (<-) which is an arrow that points to the left, created with the less-than (<) sign and the hyphen (-) sign (keyboard shortcut: Alt + - for Windows/Linux and Option + - for Mac).

For example, suppose we would like to store the number 1/40 for future use. We will assign this value to an object called x:

x <- 1/40

Notice that assignment does not print a value. Instead, R stores it for later in the object x. Call object x now and see that it contains the value 0.025:

x
[1] 0.025

If we look for the Environment tab in one of the panes of RStudio, we will see that x and its value have appeared.

 

How to print the results of assignment immediately

Surrounding the assignment with parentheses results in both assignment and print to screen to happen. For example:

(x <- 1/40)
[1] 0.025

 

Our object x can be used in place of a number in any calculation that expects a number. For example:

log(x)
[1] -3.688879

 

Use space before and after operators (Highly Recommended)

It is important the space before and after comparison operators and assignments. For example, suppose we want to code the expression x smaller than -1/50 (note that x is 1/40):

  • With spaces
x < -1/50    # with spaces 
[1] FALSE

The result is the logical FALSE because the value x (equals to 1/40) is higher than -1/50.

  • Without spaces
x<-1/50    # without spaces
x
[1] 0.02

If we omit the spaces we end up with the assignment operator and we have x <- 1/50 which equals to 0.02.

 

6.2.2 Other types of assignment

It is also possible to use the = or -> rightward operator for assignment (but these are much less common among R users).

For example:

x = 1/40
x
[1] 0.025

or

1/40 -> x
x
[1] 0.025

It is a good idea to be consistent with the operator we use.

 

6.3 Reassigning an object

Notice also that objects can be reassigned. For example, recall the x object:

x
[1] 0.025

then type the following:

x <- 100
x
[1] 100

x used to contain the value 0.025 and now it has the value 100.

Moreover, assignment values can contain the object being assigned to:

x <- x + 1 
x
[1] 101

The right hand side of the assignment can be any valid R expression and it is fully evaluated before the assignment takes place.

6.5 We are not limited to store numbers in objects

In objects we can also store other data types. For example, we can store strings of characters:

sentence <- "the cat sat on the mat"

Note that we need to put strings of characters inside quotes.

 

But the type of data that is stored in an object affects what we can do with it:

sentence + 1

Error in sentence + 1: non-numeric argument to binary operator