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
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: AltAlt + -- for Windows/Linux and OptionOption + -- 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 objectx. Call object x now and see that it contains the value0.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:
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 spacesx
[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/40x
[1] 0.025
or
1/40->xx
[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<-100x
[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+1x
[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.4 Legal object names
Object names must start with a letter and can contain letters, numbers, underscores ( _ ) and periods (.). They cannot start with a number or underscore, nor contain spaces at all. Moreover, they can not contain Reserved words.
Different people use different conventions for long object names, these include:
periods.between.words
underscores_between_words
camelCaseToSeparateWords
What we use is up to us, but we must be consistent. We might ask help:
??make.names??clean_names
R is case-sensitive
R treats capital letters differently from lower-case letters.
Y<-50Y
but…
y
Error: object ‘y’ not found
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