In this short lesson, we'll learn how to define variables in R. A variable is a name you assign a value or object, such as one of the R's atomic data types. After assigning a variable, you can access its associated value or object using the variable's name.
There are two ways to assign variables, = (the equals sign) and <- (the less than sign followed by a hyphen.).
In [1]:
x = 10 # Assign x the double 10
y <- "Life is Study" # Assign y a character
z <- (2+3)^2 # Assign z the result of (2+3)^2
x
y
z
Out[1]:
Out[1]:
Out[1]:
It is good practice to put a space between the variable, assignment operator and value to make things clear:
In [2]:
p<-8 # This works, but it looks messy.
p
p <- 10 # Use spaces!
p
Out[2]:
Out[2]:
As shown above, you can reassign a variable after creating it. After assigning variables you can perform operations on the objects assigned to them using their names.
In [3]:
x + z + p
Out[3]:
You can assign the same object to multiple variables with a multiple assignment statement.
In [4]:
r <- t <- 4 # Assigns 4 to both r and t
q = w = TRUE # Assigns TRUE to both q and w
r
t
q
w
Out[4]:
Out[4]:
Out[4]:
Out[4]:
The R community and R style guides generally prefer using <- for variable assignment. One reason for this is that the equals sign is used in places other than variable assignment statements. Functions often take named arguments and when calling a function you use the "=" symbol to assign values to named arguments. For example, recall that the function round() can take an optional argument "digits" to round a decimal to a specific number of places:
In [5]:
round(23.4567, digits = 2)
Out[5]:
In this case, the equals sign tells the function round() to use 2 as the value for the digits argument. It does NOT create a new variable called digits and assign it the value 2.
In [6]:
digits # The variable digits doesn't exist
On the other hand, <- always makes a variable assignment. For instance, Passing digits <- 2 to round() causes it to use 2 as the value for the digits argument AND assigns a new variable named digits the value 2:
In [7]:
round(23.4567, digits <- 2)
Out[7]:
In [8]:
digits # The variable digits exists
Out[8]:
Defining a new variable when calling a function in this way is usually not what you want to do. Therefore it is considered good practice to use <- for variable assignment and reserve = for assigning function arguments (and == for checking equality.).
Some people prefer using = for variable assignment because other programming languages use = and it requires fewer keystrokes. Whichever method you use, be sure to remain consistent: the last thing you want is to do is use <- some of the time and = some of the time within the same code.
*Note: RStudio has a keyboard shortcut for <- . My version of RStudio has the shortcut set to Alt+- (hold alt and then press the + and - keys simultaneously) for Windows and Option+- for Mac. In RStudio, click on the "Help" dropdown menu and then "Keyboard Shortcuts" to check your shortcuts.
You can assign all sorts of objects to variables other than the atomic data types we're familiar with. To get the most out of our variables, we need to learn about R's data structures--objects that hold multiple values that we can use to organize data and perform more interesting operations. We'll start with R's most common data structure: the vector.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.