Keywords in R

 

Keywords in R

Introduction

Keywords are reserved words in the R programming language that have predefined meanings and serve specific purposes. These words are recognized by the R interpreter and are used to perform tasks such as decision-making, looping, function definition, and representing special values. Since keywords have fixed meanings, they cannot be used as variable names, function names, or object names.

For example, the following statement is invalid because if is a keyword:

if <- 10

which results in an error because if is reserved for conditional statements.

The complete list of reserved keywords in R can be viewed using:

help(reserved)

or

?reserved

Reserved Keywords in R

The reserved keywords in R are:

if         else           repeat      while
function for      in next
break TRUE     FALSE NULL
Inf NaN     NA NA_integer_
NA_real_ NA_complex_    NA_character_

These keywords can be broadly classified into the following categories:

  1. Control Flow Keywords
  2. Function Definition Keyword
  3. Loop Control Keywords
  4. Logical Constants
  5. Missing Value Constants
  6. Special Numeric Constants
  7. Null Object

1.Control Flow Keywords

These keywords are used to control the flow of execution in a program.

(a) if

The if keyword is used to execute a block of code when a specified condition is true.

Syntax

if(condition)
{
statements
}

Example

x <- 10

if(x > 5)
{
print("Greater than 5")
}

Output

[1] "Greater than 5"

(b) else

The else keyword specifies an alternative block of code to be executed when the condition in the if statement is false.

Example

x <- 3

if(x > 5)
{
print("Greater than 5")
}
else
{
print("Less than or equal to 5")
}

Output

[1] "Less than or equal to 5"

(c) while

The while keyword repeatedly executes a block of code as long as the specified condition remains true.

Example

i <- 1

while(i <= 3)
{
print(i)
i <- i + 1
}

Output

[1] 1
[1] 2
[1] 3

(d) repeat

The repeat keyword creates an infinite loop that continues until terminated by a break statement.

Example

i <- 1

repeat
{
print(i)
i <- i + 1

if(i > 3)
break
}

Output

[1] 1
[1] 2
[1] 3

(e) for

The for keyword is used to iterate over a sequence of values.

Example

for(i in 1:5)
{
print(i)
}

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

(f) in

The in keyword is used along with the for loop to specify the sequence through which the loop variable iterates.

Example

for(i in c(10,20,30))
{
print(i)
}

Output

[1] 10
[1] 20
[1] 30

2. Function Definition Keyword

function

The function keyword is used to create user-defined functions.

Syntax

function(arguments)
{
statements
}

Example

add <- function(a,b)
{
return(a+b)
}

print(add(10,20))

Output

[1] 30

3. Loop Control Keywords

(a) break

The break keyword immediately terminates a loop.

Example

for(i in 1:10)
{
if(i==5)
break

print(i)
}

Output

[1] 1
[1] 2
[1] 3
[1] 4

(b) next

The next keyword skips the current iteration and proceeds with the next iteration of the loop.

Example

for(i in 1:5)
{
if(i==3)
next

print(i)
}

Output

[1] 1
[1] 2
[1] 4
[1] 5

 4.Logical Constants

R provides two predefined Boolean constants.

TRUE

Represents logical true.

x <- TRUE

print(x)

Output:

[1] TRUE

FALSE

Represents logical false.

x <- FALSE

print(x)

Output:

[1] FALSE

 5.Missing Value Constants

R provides several constants to represent missing values.

NA

Represents a missing or unavailable value.

x <- c(10,20,NA,40)

print(x)

Output:

[1] 10 20 NA 40

Specialized Missing Values

ConstantType
NA_integer_    Integer
NA_real_    Numeric
NA_complex_    Complex
NA_character_    Character

Example

x <- NA_character_

print(x)

Output:

[1] NA

6.Special Numeric Constants

Inf

Represents positive infinity.

Example

print(1/0)

Output

[1] Inf

NaN

Represents "Not a Number".

Produced when undefined mathematical operations occur.

Example

print(0/0)

Output

[1] NaN

7.NULL

NULL represents the absence of a value or an empty object.

Example

x <- NULL

print(x)

Output

NULL

Case Sensitivity of Keywords

R is a case-sensitive language. Therefore:

TRUE

is different from

True

Example:

print(TRUE)
print(True)

Output:

[1] TRUE

Error: object 'True' not found

Viewing Reserved Keywords

To display all reserved words in R, use:

help(reserved)

or

?reserved

Summary of Keywords in R

CategoryKeywords
Conditional Statements    if, else
Loops    for, while, repeat, in
Function Definition    function
Loop Control    break, next
Logical Constants    TRUE, FALSE
Missing Values    NA, NA_integer_, NA_real_, NA_complex_, NA_character_
Special Values    Inf, NaN, NULL

Important Points

  • Keywords are reserved words with predefined meanings.
  • They cannot be used as variable names or function names.
  • They are essential for controlling program flow and representing special values.
  • R keywords are case-sensitive.
  • The list of reserved words can be obtained using help(reserved).

Conclusion

Keywords are fundamental components of the R language that provide constructs for conditional execution, looping, function definition, loop control, and representation of special values. Since these words have predefined meanings, they are reserved by the language and cannot be redefined by the programmer. Understanding the purpose and usage of keywords is essential for writing effective and error-free R programs.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Sample Programs to Explore Basic Concepts in R

Basic Input and Output Functions in R