Course Content
R Introduction: Part I
1. Basic Syntax and Operations
R Introduction: Part I
Assignment Ways
In programming, different languages and environments offer various ways to assign values to variables. In R, you have multiple assignment operators that can be used interchangeably at this stage of learning.
In R, specific assignment operators such as <-
and ->
are available alongside the more universally recognized =
operator. At this point, the difference between these operators is not significant, but we will revisit this when we discuss functions.
Using <-
for assignment is analogous to using =
, with the variable name positioned to the left and the value to the right. Conversely, the ->
operator reverses this order. For instance, to assign the value 2020
to the variable year
, there are three syntactical options:
Even though the last method of assignment is syntactically correct, it is generally discouraged due to its potential to reduce code readability.
Note
Agreement: In subsequent chapters, we will primarily focus on using the left-arrow (
<-
) and equal (=
) for assignments.
Task
- Employ the 'equal' (
=
) operator to assign the result of integer division of29
by8
to the variable namedequal
. - Utilize the 'left-arrow' (
<-
) operator to assign the remainder of the division of29
by8
to the variableleft
. - Using the 'right-arrow' (
->
) operator, assign theequal
variable raised to the power ofleft
to theright
variable. - Display all three variable values in the order they were created with the
cat()
function.
Everything was clear?