The Wayback Machine - https://web.archive.org/web/20240906001120/https://www.geeksforgeeks.org/r-keywords/
Open In App

R – Keywords

Last Updated : 03 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
    


  • Previous Article
    Next Article

Similar Reads

Interactive Data Visualizations in R Using ggiraph
Interactive data visualizations can significantly enhance the ability to explore and understand complex datasets. In R, the ggiraph package allows you to create interactive versions of ggplot2 visualizations. This article will provide an overview of ggiraph, its key features, and step-by-step examples to create interactive plots in R Programming La
5 min read
Data analysis using R
Data Analysis is a subset of data analytics, it is a process where the objective has to be made clear, collect the relevant data, preprocess the data, perform analysis(understand the data, explore insights), and then visualize it. The last step visualization is important to make people understand what's happening in the firm. Steps involved in data
5 min read
Frequency table in R
In this article, we are going to learn how to make a frequency table in the R programming language. Table of ContentFrequency TableOne-Way Frequency Tables in RTwo-Way Frequency Tables in RFrequency TableA frequency table is a list of objects with the frequency of each item shown in the table. When evaluating categorical data to determine how frequ
5 min read
How to Find and Count Missing Values in R DataFrame
In this article, we will be discussing how to find and count missing values in the R programming language. Find and Count Missing Values in the R DataFrameGenerally, missing values in the given data are represented with NA. In R programming, the missing values can be determined by is.na() method. This method accepts the data variable as a parameter
4 min read
A Guide to dnorm, pnorm, rnorm, and qnorm in R
In this article, we will be looking at a guide to the dnorm, pnorm, qnorm, and rnorm methods of the normal distribution in the R programming language. dnorm function This function returns the value of the probability density function (pdf) of the normal distribution given a certain random variable x, a population mean μ, and the population standard
3 min read
apply(), lapply(), sapply(), and tapply() in R
In this article, we will learn about the apply(), lapply(), sapply(), and tapply() functions in the R Programming Language. The apply() collection is a part of R essential package. This family of functions helps us to apply a certain function to a certain data frame, list, or vector and return the result as a list or vector depending on the functio
4 min read
How to Change Axis Scales in R Plots?
In this article, we will learn how to change Axis Scales in the R Programming Language. Method 1: Change Axis Scales in Base R To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively. This fu
4 min read
How to Create Tables in R?
In this article, we will discuss how to create tables in R Programming Language. Method 1: Create a table from scratch We can create a table by using as.table() function, first we create a table using matrix and then assign it to this method to get the table format. Syntax: as.table(data) Example: In this example, we will create a matrix and assign
2 min read
Group by function in R using Dplyr
Group_by() function belongs to the dplyr package in the R programming language, which groups the data frames. Group_by() function alone will not give any output. It should be followed by summarise() function with an appropriate action to perform. It works similar to GROUP BY in SQL and pivot table in excel. Syntax: group_by(col,...) Syntax: group_b
2 min read
How to import an Excel File into R ?
In this article, we will discuss how to import an excel file in the R Programming Language. There two different types of approaches to import the excel file into the R programming language and those are discussed properly below. File in use: Method 1: Using read_excel() In this approach to import the Excel file in the R, the user needs to call the
3 min read
Change column name of a given DataFrame in R
A data frame is a tabular structure with fixed dimensions, of each rows as well as columns. It is a two-dimensional array like object with numerical, character based or factor-type data. Each element belonging to the data frame is indexed by a unique combination of the row and column number respectively. Column names are addressed by unique names.
5 min read
R Tutorial | Learn R Programming Language
R Programming Tutorial is designed for beginners and experts. This free R Tutorial gives you knowledge basic to advanced of concepts of the R programming language. Here you will get a detailed introduction, features, installation, variables, data types, operators, if statements, vectors, data handling, graphics, and statistical modeling of R progra
9 min read
R - Data Frames
R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. Data Frames in R Language are generic data objects of R that are used to store tabular data.  Data frames can also be interpreted as matrices where each column of a matrix can be of different data types. R DataFrame is
10 min read
Chi-Square Test in R
The chi-square test of independence evaluates whether there is an association between the categories of the two variables. There are basically two types of random variables and they yield two types of data: numerical and categorical. In R Programming Language Chi-square statistics is used to investigate whether distributions of categorical variable
4 min read
Taking Input from User in R Programming
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. Like other programming languages in R it's also possible to take input from the user. For doing so, there are two methods in R. Using readline() me
7 min read
Printing Output of an R Program
In R there are various methods to print the output. Most common method to print output in R program, there is a function called print() is used. Also if the program of R is written over the console line by line then the output is printed normally, no need to use any function for print that output. To do this just select the output variable and pres
4 min read
Read contents of a CSV File in R Programming - read.csv() Function
read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. Syntax: read.csv(file, header, sep, dec) Parameters: file: the path to the file containing the data to be imported into R. header: logical value. If TRUE, read.csv() assumes that your file has a header row, so row 1 is the n
3 min read
Data visualization with R and ggplot2
Data visualization with R and ggplot2 in R Programming Language also termed as Grammar of Graphics is a free, open-source, and easy-to-use visualization package widely used in R Programming Language. It is the most powerful visualization package written by Hadley Wickham. It includes several layers on which it is governed. The layers are as follows
9 min read
R - Statistics
Statistics is a form of mathematical analysis that concerns the collection, organization, analysis, interpretation, and presentation of data. Statistical analysis helps to make the best use of the vast data available and improves the efficiency of solutions. R – StatisticsR Programming Language is used for environment statistical computing and grap
6 min read
How to Install R and R Studio?
Navigating the R language installation process and setting up R Studio is crucial for anyone looking to delve into data analysis, statistical computing, and graphical representation with R. In this article, we provide a step-by-step guide on how to install R and configure R Studio on your system. Whether you're a beginner taking your first steps in
6 min read
R - Matrices
R-matrix is a two-dimensional arrangement of data in rows and columns. In a matrix, rows are the ones that run horizontally and columns are the ones that run vertically. In R programming, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices: Creating a Matrix in RTo create a matrix in R you need to use the
12 min read
Basic Syntax in R Programming
R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features. This article assumes R is already installed on your machin
3 min read
R Data Types
Different forms of data that can be saved and manipulated are defined and categorized using data types in computer languages, including R. Each R data type has unique properties and associated operations. What are R Data types?R Data types are used to specify the kind of data that can be stored in a variable. For effective memory consumption and pr
8 min read
Functions in R Programming
A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when you want to perform a certain task multiple times. In R Programming Language when you are creating a function the function name and the file in which you are creating the function need not be the same and
8 min read
Getting started with Data Visualization in R
Data visualization is the technique used to deliver insights in data using visual cues such as graphs, charts, maps, and many others. This is useful as it helps in intuitive and easy understanding of the large quantities of data and thereby make better decisions regarding it. Data Visualization in R Programming LanguageThe popular data visualizatio
6 min read
R Operators
Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R Operators R supports majorly four kinds of binary operators between a set of
9 min read
R Programming Language - Introduction
The R Language stands out as a powerful tool in the modern era of statistical computing and data analysis. Widely embraced by statisticians, data scientists, and researchers, the R Language offers an extensive suite of packages and libraries tailored for data manipulation, statistical modeling, and visualization. In this article, we explore the fea
7 min read
Histograms in R language
A histogram contains a rectangular area to display the statistical information which is proportional to the frequency of a variable and its width in successive numerical intervals. A graphical representation that manages a group of data points into different specified ranges. It has a special feature that shows no gaps between the bars and is simil
3 min read
R - Bar Charts
Bar charts are a popular and effective way to visually represent categorical data in a structured manner. R stands out as a powerful programming language for data analysis and visualization. In this article, we'll look at how to make visually appealing bar charts in R. Bar Charts using RA bar chart also known as bar graph is a pictorial representat
5 min read
Simple Linear Regression in R
Regression shows a line or curve that passes through all the data points on the target-predictor graph in such a way that the vertical distance between the data points and the regression line is minimum What is Linear Regression?Linear Regression is a commonly used type of predictive analysis. Linear Regression is a statistical approach for modelli
12 min read
Article Tags :