Introduction
Assignment operators let you store values in variables so you can use them later.
Assignment operators let you store values in variables so you can use them later.
variable <- value variable = value value -> variable
<- is the most common assignment operator in R.
You can also use = for assignment, but <- is preferred for clarity.
x.x <- 5name using =.name = "Alice"y using the rightward operator.10 -> yThis program assigns 10 to a, 20 to b, adds them, and stores the result in sum. Then it prints the sum.
a <- 10 b <- 20 sum <- a + b print(sum)
Use <- for assignment to follow R community style.
Assignment operators do not print the value unless you explicitly print it.
You can assign any type of data, like numbers, text, or even results of functions.
Assignment operators store values in variables.
<- is the preferred operator in R.
Variables help you reuse and update data easily.
<-.= can assign but is less preferred; == is for comparison; -> assigns in reverse direction.x == 10 is a comparison, not assignment; 10 -> x <- is invalid syntax; x =< 10 is invalid.x <- 10 correctly assigns 10 to x using preferred operator.x <- 5 y <- 3 x <- x + y print(x)
x is 5, y is 3.x <- x + y means x becomes 5 + 3 = 8.a <- 4 b <- 2 a =+ b print(a)
=+ is not a valid assignment or arithmetic operator in R.a <- a + b or a += b (not valid in base R).z after execution?x <- 2 y <- 3 z <- x z <- z * y z <- z + x
x is 2, y is 3, z is assigned value of x, so 2.z <- z * y means z = 2 * 3 = 6.z <- z + x means z = 6 + 2 = 8.Pass the test and this pattern moves unknown