Variables in R

Unlike Java or C#, R enables total flexibility in the assignment of variables. This means that you can assign objects of different types to the same variable. This will cause overriding:

var1 <- 10
var1 <- "a string"

In this case, for instance, R will not throw an error for var1. In addition, there is no need to pre-declare the class of the variable.

The assignment of variables in R can be done in the following three ways:

  • <- or ->: These arrows assign the corresponding value to a variable. However, the first alternative is more common:
    var1 <- 10
    10 -> var1
    
  • =: This is similar to <- or ->.

    Tip

    As in most programming languages, it is important to keep in mind that whenever a comparison is needed, == must be used.

  • assign(): This is a function ...

Get Learning Shiny now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.