logo

VARIABLES


Generally, while doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that, when you create a variable you reserve some space in memory.

You may like to store information of various data types like character, wide character, integer, floating point, double floating point, Boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.




In contrast to other programming languages like C and java in R, the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable. There are many types of R-objects. The frequently used ones are −

We will describe all of them in due time.

While using R for calculation, it is always advisable to store the outcome of a particular calculation against a variable. A variable may be considered as a container which will store the outcome.

The general way of declaring a variable name is

variable_name <- (the r calculation)  

For example

x<- 2+5

However in general we <- and = are equivalent and hence it is convenient to use = in place of <-.

So we can use the code

x=2+5

To store the value of 2+5 (=7) against x. We can call the variable at any time to use the value stored against the variable in our program.

For example:

x_square=x^2
x_square
[1] 49

If we simply type the variable name and press Enter it will return the value stored in that variable name. This process is called calling the variable.

Once some values are stored in a variable name , then we can subject the variables to all arithmetic operations

x=5
y=10
x+y
[1] 15
x-y
[1] -5
x*y
[1] 50
y/x
[1] 2
y^2+x
[1] 105
2*x+y-15
[1] 5

Naming a variable

There are few points that should be considered while naming a variable in R: