Reserved words ,Identifier & constants in R


January 21, 2022, Learn eTutorial
1667

In this tutorial, you will learn some of the basic concepts in R like keywords, constants, identifiers.

Most programming languages use a pre-defined set of words that specifies particular meaning upon certain actions while executing a program. This set of pre-defined words is popularly known as keywords. In R these keywords are referred to as reserved words.

The reserved word itself specifies its meaning, which means a word that is kept particularly for something. In R a reserved word denotes the words that are kept special against some programming action. R provides a list of reserved words as shown below

if

else

repeat

While

Function

for

in

next

break

TRUE

FALSE

NULL

Inf

Nan

NA

NA_integer

NA_real

NA_complex

NA_character

……

……..

  • The reserved words if-else repeat While Function  for in next  break are words reserved for loops, decision making (conditional statements), etc
  • TRUE & FALSE are logical constants.
  • NULL represents undefined values.
  • Inf  for infinity
  • Nan for not a number
  • NA is Not Available the reserved word to display when particular action cannot be performed in R.

You can view the same reserved word by typing   ?reserved or help(reserved) in the R command prompt.

Snippet showing reserved words in R console

R Resrved Words

Note: Reserved word cannot be used as identifiers.

In R programming language an identifier is a composition of alphabets (a-z, A-Z), digits (0,1,…),dot(.) or underscore(_).

Examples of acceptable identifiers in R are 


site_123, Site, site, SiteName, Sitename, .site_123, Language_1
 

Examples of identifiers that are not acceptable are


_language, Language$1, 1language, .123_site
 

Note: Refer to naming convention in the R section of variable tutorial(link to a variable) to expand your knowledge on the same topic.

Constants are the values that remain unchanged or cannot be altered during the execution of a program. R supports the user with some built-in constants which means that there are some already existing or pre-defined constants in the R language. R also supports numeric and character constants. 

R constants

Numeric constants

The numeric constants are composed of numbers and are user-defined types. They are classified as 

  1. Double
  2. Integer
  3. complex

The datatype of the function can be determined using typeof() function.

program to show numeric constants


a = 5      # assigns a numeric value 5 to a
print(typeof(a))      # determine type of a

b= 5L   #assigns integer value 5 to b
print(typeof(b))     # determine type of b

c=2+3i  #assigns a complex number to c 
print(typeof(c))    # determine type of c

 

Output:


[1] "double"
[1] "integer"
[1] "complex"

Character constants

The constants of character type are user defined using single (‘’) or double quotes (“ “).

program using character constant.


a = "Learn eTutorials"    #assigns character to a 
print(typeof(a))
print(class(a))
 

Output:


[1] "character"
[1] "character"

Note: • typeof(),class() function to check the data type of a variable

Pre-defined Constants in R

The R provides already defined constants all you have to do is take the R console and type the name of constant like pi,letters(provides lower case),LETTERS (upper case) etc as given in the below snippet which shows some of the pre-defined constants in R.

Pre-defined Constants

You can type ?constants in R prompt to find out the details of constants in R which provides result as shown in below snippet

Pre-defined Constants