R Programming Tutorial | Learn with Examples

In this R programming Tutorial with examples, you will learn what is R? its features, advantages, modules, packages, and how to use R in real-time with sample examples.

All examples provided in this R tutorial are basic, simple, and easy to practice for beginners who are enthusiastic to learn R and advance their careers.

Note: In case you can’t find the R examples you are looking for on this tutorial page, I would recommend using the Search option from the menu bar to find your tutorial and sample example code.

1. R Programming Introduction

R is a free open-source programming language mainly used for statistical computing and graphics. R is an interpreter similar to Python where you don’t have to compile first in order to run your program. Once you create your program you can just run it on a wide variety of UNIX platforms, Windows and macOS.

R was initially started by statisticians to make statistical processing easier but later other programmers are involved and evolved it to a wide variety of non-statistical tasks, including data processing, graphic visualization, and analytical processing.

Like any other programming language, R also supports extension in the form of packages hence the developers can create their own packages and re-use them where required.

2. Install R on Mac

In this section of the R tutorial, I will explain how to install R on Mac. First, let’s download the R package for Mac (macOS) from the below URL. R installer is available for Windows, Linux, Mac e.t.c. I have a dedicated article where I have explained how to install R and RStudio on Mac


Select the Download R for macOS option and choose the version you wanted to run R programs. This downloads the r*.pkg file to your system.

Once the download completes, open the downloaded folder and install the R package on Mac.

Now download rstudio IDE by accessing the below URL.


Scroll down and choose the free version to download. This takes you to the RStudio versions available to download. Choose the R version based on your mac os version. Once downloaded successfully, open it to install. Follow the instruction on the screen to complete the installation.

Open the RStudio application from the dock or from the applications and you should see something like the below screen and you will have RStudio with the prompt. From this prompt, you can run any R programming language statements.

R Tutorial

3. Hello World Program in R

Without explaining the Hello world program the R tutorial won’t be fulfilled. In order to run the hello world program in R, open RStudio IDE from your system which brings you the below RSudio application. On the IDE, go to the console tab and type print('R Hello World Program') on the prompt and press enter to execute the statement.

r tutorial

The above Hello Word program in R doesn’t exactly show the benefit of the R hence, use the R data frame, which is the main feature of the R programming language.

4. R Data Types, Operators

5. R Statements (if, loop e.t.c)

Similar to any programming language, R also provides statements like if, loop, while, and repeat e.t.c

5.1 Statement if… else

The R language supports three different ways to write if else statement, The if, if…else, and if…else…if in R. These if statements are conditional decision-making statements that are used to execute a block of code based on a condition.

Following are some examples of an if statement with a condition. You can also write R if..else with multiple conditions.

# R if statement
str <- 'Spark'
if(str == 'Spark') {

# R if...else statement
str <- 'Python'
if(str == 'Spark') {

# R if...else if
str <- 'Python'
if(str == 'Spark') {
  print('str value is Spark')
}else if(str == 'Python' ) {
  print('str value is Python')
  print('str value is not Spark and Python')

Alternatively, you can also use ifelse() function from the R base package that takes a vector as input and return a vectorized output. The ifelse() is a function that takes a vector as a test condition and executes the test condition for each element in the vector.

5.2 for loop

The for loop in R is used to repeatedly execute a set of statements or block of code for every element in a sequence (vector, list, array e.t.c).

# R for loop
numbers <- c('One','Two',"Three","Four","Five")
for(i in numbers) {

To interrupt the looping statements you can use the break and next statements in R.

Following are some other examples of using for loop.

5.3 while loop

The while loop in R is used to execute a set of statements in a loop as long as a condition is TRUE. It is a control statement.

# while example
i <- 1
n <- 5
while (i <= n) {
  i = i + 1

5.4 Repeat loop

The repeat loop statement in R is similar to do while statement in other languages. repeat run the block of statements repeatedly until the break jump statement is encountered. You have to use the break statement to terminate or exit the loop. Not using a break will end up the repeat statement in an indefinite loop.

# repeat example
i = 1
repeat {
  i = i + 1

  if(i >= 5 )

6. R Objects

Following are the different objects R supports, In this tutorial, I will give you a glimpse of these objects.

  • Vectors
  • Lists
  • Array
  • Factors
  • Matrices
  • Data Frames

7. Vectors in R

The vector in R is the simplest basic type of object in R. A Vector is a sequence of data elements of the same type. You can learn Vector from my dedicated R Vector with examples. A vector is similar to a sequence object in Python pandas. Members of a vector are called Components.

Let’s create a simple integer vector using c().

vi = c(1,3,6,7) # Integer vector

And, create a character vector and boolean vector.

vi = c('One','Two','Three') # Character Vector
vb = c('TRUE','TRUE','FALSE') # Boolean Vector

And the length of the vector can get using the length() function.


8. Matrices in R

The Matrix in R is also called Matrices which is a two-dimensional data structure that is used to store data in rows and columns. A row is a horizontal representation of the elements and a column is a vertical representation of the elements. All elements in the matrix should be of basic R type (integer, character, boolean e.t.c).

Use the matrix() function to create a two-dimensional object in R Programming Language. The following example creates a matrix with 3 rows and 3 columns.

# Create R matrix
data <- c(10,11,12,13,14,15,16,17,18)
mtx <- matrix(data,nrow=3,ncol=3,byrow=TRUE)

Yields below output. As you see above matrix is a collection of two or more Vectors.

     [,1] [,2] [,3]
[1,]   10   11   12
[2,]   13   14   15
[3,]   16   17   18

9. List in R

In R, List objects are used to store elements of different types like numbers, strings, vectors, and even lists. The good thing in R is, elements in List can be given names and they can be accessed using these names.

You can use the dollar sign in R to access, update, insert, and delete elements from the named List.

10. Arrays in R

11. Factors in R

12. DataFrame Tutorial in R

An R data frame represents the data in rows and columns similar to Python pandas DataFrame and SQL. Each column in the data frame is a vector of the same length, in other words, all columns in the data frame should have the same length.

In the R data frame columns are referred to as variables and rows are referred to as observations. Refer to the R Data Frame Tutorial where I covered several examples with explanations of working with data frames. In this R programming tutorial section, I will just give you a glimpse look how the DataFrame looks and how to create it.

Let’s create an R data frame by using data.frame() function. Dataframe in R stores the data in the form of rows and columns similar to RDBMS tables. So it is a two-dimensional data structure such that one dimension refers to the row and another dimension refers to a column. I will cover more on the data frame in the following sections.

#Create dataframe

#Display the dataframe

Yields the output in a table.


13. Run R script Using rscript Command

Running R programs from an RStudio would be helpful during the development where you wanted to run the statements and validate the output. But in real-time we would write the r programs in the R script file with an extension .R and run it from the command line.

Open your favorite text editor and create a helloworld.r file with the data frame and print statements (explained in section 2).

r programming tutorial
R script file

Now open the terminal or command prompt and run the r script file using rscript command. If you stored the file in a custom path then use the absolute path of the script to execute. You can also create an R program in RStudio, save the file to the disk and run it using rscript command.

r programming
Execute rscript from the command line

14. R Base Functions

There are several built-in R base functions

15. R Packages

In order to use these packages, you have to install R packages first using install.packages('<package>') and load them using library(<package>). Below are some of the most used R packages to learn and explore. Click on each item below to get to the tutorial for each R package.

If you already have these packages and to update to the latest version either remove the package and install it again or update it using update.packages()

15. Conclusion

In this R Programming Tutorial, you have learned what is R, its usage, and how to install it and run the hello world program. Also, it learned data structures it supports like Vector, Matrix, and Data Frame. Finally looked into different R packages.