# R Variables, Constants and Vectors

Variables and constants are the fundamental units that are used to develop a program. Almost all programming languages provide the feature to make use of variables and constants. In this chapter you will learn about the concepts of variables, constants and some basic methods of using vectors within a R program.

## What are Variables in R

Variables are used for storing data where that value can be altered based to your need. Unique name has to be given to variable (also for functions and objects) is identifier.

## Rules for writing Identifiers in R

Identifier names are a combination of alphabets, digits, period (.) and also underscore (_). It is mandatory to start an identifier with a letter or a period. Another thing is if it starts with a period / dot operator then it you cannot write digit following it. Here are some of the points other than that which you should remember before naming any identifier:

• Reserved words in R cannot be used as identifiers.
• Valid identifiers in R are:
total, Sum, .work.with, this_is_accepted, Num6
• Invalid identifiers in R:
[email protected], 5um, _ray, TRUE, .0n3

### Best Practices for Writing Identifiers

Former versions of R used underscore to assign values. So, the period (.) operator was used broadly in variable names that have multiple words. Present versions of R support underscore (_) as valid identifier. But it is considered to be not a good practice to use period for word separators. Here's an example, a.variable.name is preferred over a_variable_name.

## What are Constants in R

Constants are entities within a program whose value can't be changed. There are 2 basic types of constant. These are numeric constants and character constants.

### Numeric Constants

All the numbers you will be using within a program fall under this category. There are sub types like integer, double or complex, which is checked usng typeof() function. Example:

``````> typeof (6)
 "double"

> typeof (4L)
 "integer"``````

### Character Constants

These can be signified by means of either single quotes (') or using double quotes (") as delimiters.

``````> 'ray'
 "ray"

> typeof("karlos")
 "character"``````

## What are Vectors in R Programming?

So far, you have used the colon operator in previous chapter, to create series from one number to another, the c function used for concatenating different values and vectors for creating longer vectors.
Let's recall the statements back:

``````8.4:4.6       #sequence of numbers from 8.4 down to 4.6

##  8.5 7.5 6.5 5.5 4.5
c (1, 1:3, c (5, 8), 13)       #values concatenated into single vector

##  1 1 2 3 5 8 13``````

The vector function is used to create a vector of a fixed type and length. Every value has the result as zero, FALSE or any null string or something that is equivalent of "nothing". Here is a list of examples:

``````vector ("numeric", 4)
##  0 0 0 0
vector ("complex", 4)

##  0+0i 0+0i 0+0i 0+0i
vector ("logical", 4)

##  FALSE FALSE FALSE FALSE

vector ("character", 4)
##  "" "" "" ""`````` 