Basics of Variables in R Programming for Predictive Analytics

Because it was designed for statistical analysis, the programming language R can be very useful for predicting outcomes based on a set of data. In order to use R, you first must know how to use variables in the language.

How to assign variables in R

Variables can be assigned in various ways in R. The convention for R is to use the less-than sign (<) and the minus sign (–) together, making an arrowlike sign (<–).

Another way (standard for other programming languages) is to use just the equal sign (=). One other feature in R is that you may assign a variable in either direction using less-than and greater-than symbols with the minus sign to make the arrowlike sign (<– or –>).

The R interpreter has a leading > sign as the command prompt in the console window. You cannot copy and paste whole sections of code in that window. It will output an error if you do. You must use the script window of RStudio if you want to copy and paste sections of code.

You can type the code that prints out the variable in one of three ways:

> x <- "hello there"
> x = "hello there"
> "hello there" -> x

The preceding three lines of code all do the same thing; knowing all the different ways will come in handy because you’ll surely come across all of them when you start reading other people’s code. As mentioned previously, the preferred way in R is the first one shown.

To print out the value of the variable, simply type the variable and then press Enter, like this:

> x
[1] "hello there"

In the RStudio pane, a line that has a number between [ ] symbols shows you the output from the execution of the previous line(s) of code.

How to operate on variables in R

For most of its arithmetic and logical operators, R uses syntax that is standard to most other programming languages.

  • Here are examples of the arithmetic operators in R:

    > w <- 5 + 5   # addition
    > x <- w * 5   # multiplication
    > y <- x / 5   # division
    > z <- y - 5   # subtraction

    The # symbol is the start of a comment; the R interpreter ignores comments. You can print out the values by using the concatenate function, like this:

    > c(w,x,y,z)
    [1] 10 50 10 5
  • Here are examples of the logical operators in R:

    w == y         # is equal to
    x > z          # is greater than
    y >= 10        # is greater than or equal to
    z < w          # is less than
    10 <= y        # is less than or equal to

    All these comparisons evaluate to TRUE values; you can see the results by executing them:

    > w == y
    [1] TRUE
    > x > z
    [1] TRUE
    > y >= 10
    [1] TRUE
    > z < w
    [1] TRUE
    > 10 <= y
    [1] TRUE

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com