Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Statistics LibreTexts

2.5: Variables

( \newcommand{\kernel}{\mathrm{null}\,}\)

A variable is a symbol that stands for another value (just like “X” in algebra). We can create a variable by assigning a value to it using the <- operator. If we then type the name of the variable R will print out its value.

> x <- 4
> x
[1] 4

The variable now stands for the value that it contains, so we can perform operations on it and get the same answer as if we used the value itself.

> x + 3
[1] 7
> x == 5
[1] FALSE

We can change the value of a variable by simply assigning a new value to it.

> x <- x + 1
> x
[1] 5

A note: You can also use the equals sign = instead of the <-


This page titled 2.5: Variables is shared under a CC BY-NC 2.0 license and was authored, remixed, and/or curated by Russell A. Poldrack via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?