# 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

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

http://lib.stat.cmu.edu/R/CRAN/

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.

https://www.rstudio.com/products/rstudio/download/.

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.

## 3. Hello World Program in R

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.

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)

## 6. R 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.

```
length(c('One','Two','Three'))
```

## 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)
mtx
```

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.

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
my_dataframe=data.frame(
"id"=c(11,22,33,44,55),
"pages"=c(32,45,33,22,56),
"name"=c("spark","python","R","java","jsp"),
"chapters"=c(76,86,11,15,7),
"price"=c(144,553,321,567,890)
)
#Display the dataframe
print(my_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).

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.

## 14. 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.

- dplyr
- tidyr
- data.table
- tidyverse
- stringr
- tibble
- sparkly
- sqldf

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.