# R – Variables & Constants

Variable is memory location for storing values in any programming language including R. In R Program variable names are assigned with symbol left arrow (<-) or right arrow (->) on either side or with equal to (=) symbol.

Variables are of 2 types based on their usage across the function

• Global Variable – Accessible from outside a function and is assigned with the symbol (<<-)
• Local Variable – Accessible only from inside a function and is assigned with the symbol (<-)

Based on the type of values they contain; variables can be classified as:

Boolean Variable

These variable have a binary value [1,0], [TRUE, FALSE], [YES, NO].

Example:

X= YES

Y= FALSE

Integer Variable

R has mostly numeric integer variable for declaration

Example:

X =5

Y = 7.5321

Character Variable

These variables are defined in quotation marks and contains either numeric or character values

Example:

X=” A”

Y= “9”

String Variable

These variables contain both numeric and character values with more than one value

Example:

M=”ABC”

N=”324”

Naming Convention of Variable

• A variable name consists of numbers, letters, dot, underline characters
• A variable name starts with a letter or a dot not followed by a number

Create a variable

Create a dataset

Stadium <- c (“Wankhede”, “Eden Gardens”)

Audience <- c (80000, 90000)

Ticket <- c (3000, 5000)

Df <- data.frame (Stadium, Audience, Ticket)

Add a new variable: Df\$stands <- c( 15, 12)

Rename an existing variable: Df\$Arena <- Df\$Stand

Delete a variable: DF\$Arena <- NULL

Constants:

Constants are assigned with values which cannot be changed. These are of 2 types (i) Numeric Constant (ii) Character Constant

Numeric Constant: They can be of type integer, double, complex

Character Constant: They are represented by using single quotes or double quotes.

Typeof () function is used to identify the type of constant

Example:

Typeof(12) – “double”

Typeof(12L) – “integer”

Typeof(12i)— “complex”

Typeof(“12) – “character”