Home / Programs / Variable assignment in R
Programming Example

Variable assignment in R

👁 344 Views
💻 Practical Program
📘 Step by Step Learning
A basic concept in (statistical) programming is called a variable.

Program Code

# Assign the value 42 to x
x <- 42

# Print out the value of the variable x
x

Output

> # Assign the value 42 to x
> x <- 42
> 
> # Print out the value of the variable x
> x
[1] 42

Explanation

A basic concept in (statistical) programming is called a variable.

A variable allows you to store a value (e.g. 4) or an object (e.g. a function description) in R. You can then later use this variable's name to easily access the value or the object that is stored within this variable.

You can assign a value 4 to a variable my_var with the command

my_var <- 4

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.