Open In App

R – Keywords

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

R is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge tool.

Keywords are specific reserved words in R, each of which has a specific feature associated with it. Almost all of the words which help one to use the functionality of the R language are included in the list of keywords. So one can imagine that the list of keywords is not going to be a small one! In R, one can view these keywords by using either help(reserved) or ?reserved. Here is the list of keywords in R:

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

Following are some most important keywords along with their examples:

  • if: If statement is one of the Decision-making statements in the R programming language. It is one of the easiest decision-making statements. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
    Example:




    # R program to illustrate if statement 
      
    # assigning value to variable a 
    a <- 5
      
    # condition 
    if( a > 0 ) 
        print("Positive Number") # Statement 

    
    

    Output:

    Positive Number
    
  • else: It is similar to if statement but when the test expression in if condition fails, then statements in else condition are executed.
    Example:




    x <- 5
      
    # Check value is less than or greater than 10 
    if(x > 10)
        print(paste(x, "is greater than 10")) 
    }
    else
        print(paste(x, "is less than 10")) 
    }

    
    

    Output:

    [1] "5 is less than 10"
    
  • while: It is a type of control statement which will run a statement or a set of statements repeatedly unless the given condition becomes false. It is also an entry controlled loop, in this loop the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false.
    Example:




    # R program to demonstrate the use of while loop 
      
    val = 1
      
    # using while loop 
    while (val <= 5 ) 
        # statements 
        print(val) 
        val = val + 1

    
    

    Output:

    [1] 1
    [1] 2
    [1] 3
    [1] 4
    [1] 5
    
  • repeat: It is a simple loop that will run the same statement or a group of statements repeatedly until the stop condition has been encountered. Repeat loop does not have any condition to terminate the loop, a programmer must specifically place a condition within the loop’s body and use the declaration of a break statement to terminate this loop. If no condition is present in the body of the repeat loop then it will iterate infinitely.
    Example:




    # R program to demonstrate the use of repeat loop 
      
    val = 1
      
    # using repeat loop 
    repeat 
        # statements 
        print(val) 
        val = val + 1
          
        # checking stop condition 
        if(val > 5) 
        
            # using break statement 
            # to terminate the loop 
            break
        

    
    

    Output:

    [1] 1
    [1] 2
    [1] 3
    [1] 4
    [1] 5
    
  • for: It is a type of control statement that enables one to easily construct a loop that has to run statements or a set of statements multiple times. For loop is commonly used to iterate over items of a sequence. It is an entry controlled loop, in this loop the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false.
    Example:




    # R program to demonstrate the use of for loop 
      
    # using for loop 
    for (val in 1:5) 
        # statement 
        print(val) 

    
    

    Output:

    [1] 1
    [1] 2
    [1] 3
    [1] 4
    [1] 5
    
  • function: Functions are useful when you want to perform a certain task multiple number of times. In R functions are created using function keyword.
    Example:




    # A simple R function to check 
    # whether x is even or odd 
      
    evenOdd = function(x){ 
    if(x %% 2 == 0) 
        return("even"
    else
        return("odd"
      
    print(evenOdd(4)) 
    print(evenOdd(3)) 

    
    

    Output:

    [1] "even"
    [1] "odd"
    
  • next:Next statement in R is used to skip any remaining statements in the loop and continue the execution of the program. In other words, it is a statement that skips the current iteration without loop termination.
    Example:




    # R program to illustrate next in for loop 
      
    val <- 6:11
      
    # Loop 
    for (i in val) 
        if (i == 8) 
        
            # test expression 
            next
        
        print(i) 

    
    

    Output:

    [1] 6
    [1] 7
    [1] 9
    [1] 10
    [1] 11
    
  • break: The break keyword is a jump statement that is used to terminate the loop at a particular iteration.
    Example:




    # R Break Statement Example 
    a<-1    
    while (a < 10) 
    {     
        print(a)     
        if(a == 5)     
            break    
        a = a + 1    
    }     

    
    

    Output:

    [1] 1
    [1] 2
    [1] 3
    [1] 4
    [1] 5
    
  • TRUE/FALSE: The TRUE and FALSE keywords are used to represent a Boolean true and Boolean false. If the given statement is true, then the interpreter returns true else the interpreter returns false.
    Example:




    # A simple R program 
    # to illustrate TRUE / FALSE  
      
    # Sample values 
    x = 4
    y = 3
      
    # Comparing two values 
    z = x > y 
    p = x < y
      
    # print the logical value 
    print(z) 
    print(p)

    
    

    Output:

    [1] TRUE
    [1] FALSE
    
  • NULL: In R, NULL represents the null object. NULL is used to represent missing and undefined values. NULL is the logical representation of a statement which is neither TRUE nor FALSE.
    Example:




    # A simple R program 
    # to illustrate NULL
      
    v = as.null(c(1, 2, 3, 4))
    print(v)

    
    

    Output:

    NULL
    
  • Inf and NaN: In R is.finite and is.infinite return a vector of the same length as x, where x is an R object to be tested. This indicating which elements are finite (not infinite and not missing) or infinite. Inf and -Inf keyword mean positive and negative infinity whereas NaN keyword means ‘Not a Number’.




    # A simple R program 
    # to illustrate Inf and NaN
      
    # To check Inf
    x = c(Inf, 2, 3)
    print(is.finite(x))
      
    # To check NaN
    y = c(1, NaN, 3)
    print(is.nan(y))

    
    

    Output:

    [1] FALSE  TRUE  TRUE
    [1] FALSE  TRUE FALSE
    
  • NA: NA stands for “Not Available” and is used to represent missing values. There are also constants NA_integer_, NA_real_, NA_complex_ and NA_character_ of the other atomic vector types which support missing values and all of these are reserved words in the R language.




    # A simple R program 
    # to illustrate  NA
      
    # To check NA
    x = c(1, NA, 2, 3)
    print(is.na(x))

    
    

    Output:

    [1] FALSE  TRUE FALSE FALSE
    


  • Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments