R Lang: Zero to Hero (2024)

A guide for statistical computing and graphics.

R is a computing environment that combines a programming language called S, that implements the idea of programming with data. It has powerful numerical analysis tools for linear algebra, differential equations, and stochastics. R Programming lets you learn about data visualisations by offering a set of inbuilt functions and libraries. There are many graphical front-ends for R. At present, the best of these appears to be RStudio. Before learning about these, however, you should learn a little about R itself.

  1. R is flexible.
  2. R is powerful.
  3. R is not just a statistics computing package; it’s a programming language.
  4. R is designed to operate the way that problems are designed for.
  5. R is efficient in handling data and storage facility.

The snippets of code that are written in the R language are always available to the user, so a small change to the task usually requires only a minor change to the code — a difference that can be carried out in a lesser amount of time.

Installing R on your machine

To download R, please choose your preferred CRAN mirror.

RStudio makes R easier to use. It includes a code editor, debugging & visualisation tools. It is an (IDE) which includes a console, syntax-highlighting editor that supports direct code execution, as well as tools for plotting, history, debugging and workspace management.

Download R-Studio from the following link.

R Lang: Zero to Hero (3)

After installing R on your machine, then start the R interactive Shell by hitting R in the terminal or shell. The default prompt is ‘>’, which on UNIX might be the same as the shell prompt, and so it may appear that nothing is happening, it will be waiting for the user for the instruction.

You can use the R Shell as a calculator, you can just play with from doing simple math to advance Machine Learning Algorithms.

To quit the R program the command is

> q()

This will exit from the R Shell you can also save the workspace session by hitting “y” after exiting if to wish to continue without saving just hit “n”, which brings you back to the terminal/shell.

Lets’s get started with printing “Hello World!” in the R interactive Shell.

In R we use print() function to return the String given in the argument.

$R
> print("Hello World!")
[1] "Hello World!"
>

As we know every programming language needs comments, R supports comments, which are ignored by the interpreter.

This it how we use comments in R.

# This is a comment in R
# print("This doesn't work")

In programming, a variable is nothing more than a name for something. A variable in R can store an atomic vector, group of atomic vectors or a combination of many R objects. R names are case sensitive. To create names for the data structures, we have to follow the following rules:

Note: Names that begin with . are considered system names and are not always visible using the ls()-function.

Now let’s see how we declare variables in R

a <- 3

This line of code declares a variable “a” and assigns it to the value 3.

typeof() function returns the datatype of the variable.

type(a)
[1] "double"

Numeric (real or decimal): Decimal values are called numerics in R. It is the default computational data type.

Integer: In order to create an integer variable in R, we invoke the as integer function.

Character: A character object is used to represent string values in R. We convert objects into character values with the as character() function.

Logical: A logical value is often created via comparison between variables.

Complex: A complex value in R is defined via the pure imaginary value i.

A code snippet explaining datatypes.

To execute R files from the shell we use the command “ Rscript filename.R ”

[1] 3
[1] "double"
[1] "Stark"
[1] "character"
[1] TRUE
[1] "logical"
[1] 1+4i
[1] "complex"

Note: Strings in R Language are Characters.

All the basic operations like addition, subtraction, division, multiplication. Etc Can be performed in R.

[1] 6
[1] 1
[1] 315
[1] 8.333333
[1] 3
[1] 1
[1] 125

Atomic Vectors

A vector is the most commonly used data structure in R . It is a sequence of data elements of the same basic type. Members in a vector are officially called components. A vector can be a vector of elements that are most commonly character, logical, integer or numeric.

We use vector() function to create an empty vector, the below snippet show’s how we declare a vector.

x <- vector()
> character(5)
[1] "" "" "" "" ""

Lists

Lists in R lang act as containers. They are generic vector containing other objects. Unlike atomic vectors, the variables of a list are not limited to a single mode and can include any mixture of data types. A list can contain other lists. This makes them unique from atomic vectors.

Lists in R are created using list() function.

my_list <- list("Red", TRUE, 51.23)
[1] "Red"
[[2]]
[1] TRUE
[[3]]
[1] 51.23
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Sun"
[[5]]
[1] "Mon"
[[6]]
[1] "Tue"

Matrix

Matrices are special types of Vectors in R. A matrix is a collection of data elements arranged in a two-dimensional rectangular layout. Matrices have rows and columns.

Now let’s create a 2x2 matrix, we use matrix function and send the rows and columns as parameters. The number of rows will be nrow, the number of columns will be ncol.

my_matrix <- matrix(nrow = 2, ncol = 2)
 [,1] [,2] [,3] [,4]
[1,] 59.0 22.3 31.2 9.5
[2,] 55.0 19.7 30.4 13.8
[3,] 53.5 20.8 30.6 14.8
[4,] 55.0 20.3 30.3 15.2
[5,] 52.5 20.8 30.3 15.5
[6,] 57.5 21.5 30.8 15.6
[7,] 53.0 20.6 32.5 15.6
[8,] 55.0 21.5 34.0 15.7

Data frame

Dataframes are on of the most used data structures in R Language. The data is represented in a tabular format which has number of rows and columns.

We usually read csv files into a data frame to do that we use read.csv() function or read.table() function and send the csv file name as an argument.

We can also create a data frame using data.frame() function.

> df <- data.frame(id = letters[1:5], x = 1:10, y = rnorm(10))
> df
> ## id x y
> ## 1 a 1 -1.37593
> ## 2 b 2 0.47094
> ## 3 c 3 -0.16046
> ## 4 d 4 -1.36914
> ## 5 e 5 0.39763

These are few import functions we apply to a data frame to draw minimal conclusions.

head() — see first 6 rows

tail() — see last 6 rows

dim() — see dimensions

nrow() — number of rows

ncol() — number of columns

str() — structure of each column

Factors

Factors are pretty much integers that have labels on them. While factors look (and often behave) like character vectors, they are actually integers under the hood, and you need to be careful when treating them like strings. Some string methods will coerce factors to strings, while others will throw an error.

Factors can be created with factor(). Input is generally a character vector.

> x <- factor(c("yes", "no", "no", "yes", "yes"))
> x
[1] yes no no yes yes
Levels: no yes
# table(x) will return a frequency table.

These allow you to control the flow of execution of a script typically inside of a function. Common ones include:

  1. if, else
  2. for
  3. while
  4. repeat
  5. break

We always need the ability to check the conditions and change the behavior of the program accordingly. The conditional statements give us the ability; the simplest form is the ‘if’ statement.

if (condition) {
# do something
} else {
# do something else
}

Example,

x is less than 10

The for loop in R has the ability to iterate over items of any sequence such as list or vectors.

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

This is how we declare for loop in R, it takes the iterable variable(i) and iterates until the range given (10, here)

1
2
3
4
5

Few ways of implementing for loops.

[1] NA
[1] NA
[1] NA
[1] NA
[1] "apples"
[1] "oranges"
[1] "bananas"
[1] "strawberries"
[1] "apples"
[1] "oranges"
[1] "bananas"
[1] "strawberries"
[1] "apples"
[1] "oranges"
[1] "bananas"
[1] "strawberries"

A while loop statement in R lang repeatedly executes a target statement as long as the given condition is true. Unlike the for loop, the while loop will not run n times, but until a defined condition is met.

Syntax of While loop

while(condition){
statements
iteration
}

Here’s an example of how we can implement a simple while loop,

1
2
3
4
5

A repeat loop is used to iterate over a block of code multiple number of times.There is no condition check in repeat loop to exit the loop.

Syntax for repeat and break

repeat{
condition
statements
break
}

Now let’s print first five numbers using repeat and break.

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

The main use of a function in any programming language is reusability. They are the set of statements organised together to perform a specific task. There are various inbuilt functions in R. Some of the inbuilt functions in R are sum(), min(), max(),mean(), etc.

Syntax for declaring a function in R

function_name <- function(arg_1, arg_2, ...) {
function body
}

Now let’s create a function to print cubes of numbers in sequence.

216

Visualising the data is one of the vital solutions for decision making. R Programming language offers a one of the best set of inbuilt function and libraries (such as ggplot2, leaflet, lattice) for creating data stories and visualisation.

Now, let’s create a simple line plot using ggplot2 in RStudio, to do that we need to install the ggplot2 package, you will find the console in the left corner. Run the command install.packages(“package_name”).

> install.packages("ggplot2")

We will now import a inbuilt dataset(mpg), and plot a simple graph.

About mpg dataset: It’s a fuel economy data from 1999 and 2008 for 38 popular models of car.

  1. A data frame with 234 rows and 11 variables
  2. displ — engine displacement, in litres
  3. hwy — highway miles per gallon
R Lang: Zero to Hero (4)
R Lang: Zero to Hero (2024)
Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 6379

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.