Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views21 pages

1 - R Language

The document provides an overview of the R programming language, highlighting its advantages for data analysis, visualization, and machine learning, as well as installation instructions. It covers key concepts such as variables, data types, operators, and control structures like loops and conditionals. Additionally, it explains how to perform mathematical operations and manage strings in R.

Uploaded by

star.rana.100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

1 - R Language

The document provides an overview of the R programming language, highlighting its advantages for data analysis, visualization, and machine learning, as well as installation instructions. It covers key concepts such as variables, data types, operators, and control structures like loops and conditionals. Additionally, it explains how to perform mathematical operations and manage strings in R.

Uploaded by

star.rana.100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

R Language

Why Use R?
● It is a great resource for data analysis, data visualisation, data
science and machine learning
● It provides many statistical techniques (such as statistical tests,
classification, clustering and data reduction)
● It is easy to draw graphs in R, like pie charts, histograms, box plot,
scatter plot, etc++
● It works on different platforms (Windows, Mac, Linux)
● It is open-source and free
● It has a large community support
● It has many packages (libraries of functions) that can be used to
solve different problems

How to Install R

Step1- To install R, go to https://cloud.r-project.org/

Step2- Download the latest version of R for Windows, Mac or Linux.


Step3- Click on ‘base’

Step4- Download the latest version.

Step5- Open the download folder and install the downloaded file.
Step6- Open the desktop and locate the installed file.

Step7- Double click on the icon to open it.

R print Output
To output text in R, use single or double quotes
To output numbers, just type the number (without quotes)
To do simple calculations, add numbers together

Singleline Comments

Comments can be used to explain R code, and to make it more readable. It


can also be used to prevent execution when testing alternative code.

Comments starts with a #. When executing code, R will ignore anything


that starts with #.

This example uses a comment before a line of code:

Multiline Comments
Unlike other programming languages, such as Java, there are no syntax in
R for multiline comments. However, we can just insert a # for each line to
create multiline comments:
R Variables

Creating Variables in R
R does not have a command for declaring a variable. A variable is created
the moment you first assign a value to it. To assign a value to a variable,
use the <- sign. To output (or print) the variable value, just type the
variable name:

From the example above, name and age are variables, while "Sujoy" and
26 are values.

NOTE- In other programming languages, it is common to use = as an


assignment operator. In R, we can use both = and <- as assignment
operators. However, <- is preferred in most cases because the = operator
can be forbidden in some context in R.

Print / Output Variables


Compared to many other programming languages, you do not have to use
a function to print/output variables in R. You can just type the name of the
variable.

However, R does have a print() function available:


R Concatenate Elements
You can also concatenate, or join, two or more elements, by using the
paste() function.

● To combine both text and a variable, R uses comma (,)


● You can also use , to add a variable to another variable
● For numbers, the + character works as a mathematical operator
● If you try to combine a string (text) and a number, R will give you an
error.

Multiple Variables

R allows you to assign the same value to multiple variables in one line:
Variable Names
A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume). Rules for R variables are:

● A variable name must start with a letter and can be a combination of


letters, digits, period(.) and underscore(_). If it starts with period(.),
it cannot be followed by a digit.
● A variable name cannot start with a number or underscore (_)
● Variable names are case-sensitive (age, Age and AGE are three
different variables)
● Reserved words cannot be used as variables (TRUE, FALSE, NULL,
if...)

# Legal variable names:


myvar <- "Sujoy"
my_var <- "Sujoy"
myVar <- "Sujoy"
MYVAR <- "Sujoy"
myvar2 <- "Sujoy"
.myvar <- "Sujoy"

# Illegal variable names:


2myvar <- "Sujoy"
my-var <- "Sujoy"
my var <- "Sujoy"
_my_var <- "Sujoy"
my_v@ar <- "Sujoy"
TRUE <- "Sujoy"

R Data Types

Data Types
In R, variables do not need to be declared with any particular type, and
can even change type after they have been set:
Basic Data Types

Basic data types in R can be divided into the following types:

● numeric - (10.5, 55, 787)


● integer - (1L, 55L, 100L, where the letter "L" declares this as an
integer)
● complex - (9 + 3i, where "i" is the imaginary part)
● character (a.k.a. string) - ("k", "R is exciting", "FALSE", "11.5")
● logical (a.k.a. boolean) - (TRUE or FALSE)

We can use the class() function to check the data type of a variable:
R Numbers

Numbers

There are three number types in R:

● numeric
● integer
● complex

Variables of number types are created when you assign a value to them:

Numeric

A numeric data type is the most common type in R, and contains any
number with or without a decimal, like: 10.5, 55, 787:

Integer

Integers are numeric data without decimals. This is used when you are
certain that you will never create a variable that should contain decimals.
To create an integer variable, you must use the letter L after the integer
value:

Complex

A complex number is written with an "i" as the imaginary part:

Type Conversion

You can convert from one type to another with the following functions:

● as.numeric()
● as.integer()
● as.complex()
R Math

Simple Math

In R, you can use operators to perform common mathematical operations


on numbers.

● The + operator is used to add together two values:


● The - operator is used to subtract two values:
● The * operator is used to multiply two values:
● The / operator is used to divide two values:
Built-in Math Functions
R also has many built-in math functions that allows you to perform
mathematical tasks on numbers. the

● min() and max() functions can be used to find the lowest or highest
number in a set
● The sqrt() function returns the square root of a number
● The abs() function returns the absolute (positive) value of a number
● The ceiling() function rounds a number upwards to its nearest
integer, and the floor() function rounds a number downwards to its
nearest integer, and returns the result

R Strings

String Literals

Strings are used for storing text.

A string is surrounded by either single quotation marks, or double


quotation marks:

"hello" is the same as 'hello':


Assign a String to a Variable

Assigning a string to a variable is done with the variable followed by the <-
operator and the string:

Multiline Strings

You can assign a multiline string to a variable like this:

NOTE- However, note that R will add a "\n" at the end of each line break.
This is called an escape character, and the n character indicates a new line.

If you want the line breaks to be inserted at the same position as in the
code, use the cat() function:

String Function
● To find the number of characters in a string, use the nchar()
function.
● Use the grepl() function to check if a character or a sequence of
characters are present in a string
● Use the paste() function to merge/concatenate two strings

R Escape Characters

To insert characters that are illegal in a string, you must use an escape
character.

An escape character is a backslash \ followed by the character you want to


insert.

To fix this problem, use the escape character \":

Other escape characters in R:


R Booleans / Logical Values

Booleans (Logical Values)

In programming, you often need to know if an expression is true or false.

You can evaluate any expression in R, and get one of two answers, TRUE or
FALSE.

When you compare two values, the expression is evaluated and R returns
the logical answer:
R Arithmetic Operators

Arithmetic operators are used with numeric values to perform common


mathematical operations:

R Assignment Operators

Assignment operators are used to assign values to variables:

Note: <<- is a global assigner.


It is also possible to turn the direction of the assignment operator.

x <- 3 is equal to 3 -> x

R Comparison Operators

Comparison operators are used to compare two values:

R Logical Operators

Logical operators are used to combine conditional statements:

R Miscellaneous Operators

Miscellaneous operators are used to manipulate data:


R If ... Else

In this example, a is greater than b, so the first condition is not true, also
the else if condition is not true, so we go to the else condition and print
to screen that "a is greater than b".

You can also use else without else if

Nested If Statements

You can also have if statements inside if statements, this is called nested
if statements.

R - AND OR Operators
● The & symbol (and) is a logical operator, and is used to combine
conditional statements.
● The | symbol (or) is a logical operator, and is used to combine
conditional statements.

Loops

Loops can execute a block of code as long as a specified condition is


reached.

Loops are handy because they save time, reduce errors, and they make
code more readable.

R has two loop commands:

● while loops
● for loops

R While Loops

With the while loop we can execute a set of statements as long as a


condition is TRUE:
In the example above, the loop will continue to produce numbers ranging
from 1 to 5. The loop will stop at 6 because 6 < 6 is FALSE.

The while loop requires relevant variables to be ready, in this example we


need to define an indexing variable, i, which we set to 1.

Break

With the break statement, we can stop the loop even if the while condition
is TRUE:

The loop will stop at 3 because we have chosen to finish the loop by using
the break statement when i is equal to 4 (i == 4).

Next

With the next statement, we can skip an iteration without terminating the
loop:
When the loop passes the value 3, it will skip it and continue to loop.

R For Loop

For Loops

A for loop is used for iterating over a sequence:

Nested Loops

It is also possible to place a loop inside another loop. This is called a


nested loop:

You might also like