Statistics With R Unit 1
Statistics With R Unit 1
Introduction to R Programming
What is R
R is a popular programming language used for statistical computing and
graphical presentation.
Why Use R?
It is a great resource for data analysis, data visualization, 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
Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do
different things.
In R, variables do not need to be declared with any particular type, and can
even change type after they have been set:
Character “a”, “b”, “c”, …, “@”, “#”, “$”, …., “1”, “2”, …etc
Numeric Datatype
Decimal values are called numerics in R. It is the default data type for
numbers in R. If you assign a decimal value to a variable x as follows, x will
be of numeric type.
# A simple R program
# to illustrate Numeric data type
# Assign a decimal value to x
x = 5.6
# print the class name of variable
print(class(x))
# print the type of variable
print(typeof(x))
Integer Datatype
R supports integer data types which are the set of all integers. You can
create as well as convert a value into an integer type using
the as.integer() function. You can also use the capital ‘L’ notation as a suffix
to denote that a particular value is of the integer data type.
# A simple R program
# to illustrate integer data type
# Create an integer value
x = as.integer(5)
# print the class name of x
print(class(x))
# print the type of x
print(typeof(x))
# Declare an integer by appending an L suffix.
y = 5L
# print the class name of y
print(class(y))
# print the type of y
print(typeof(y))
Logical Datatype
R has logical data types that take either a value of true or false. A logical
value is often created via a comparison between variables.
# A simple R program
# to illustrate logical data type
# Sample values
x = 4
y = 3
# Comparing two values
z = x > y
# print the logical value
print(z)
# print the class name of z
print(class(z))
# print the type of z
print(typeof())
Complex Datatype
R supports complex data types that are set of all the complex numbers. The
complex data type is to store numbers with an imaginary component.
# A simple R program
# to illustrate complex data type
# Assign a complex value to x
x = 4 + 3i
# print the class name of x
print(class(x))
# print the type of x
print(typeof(x))
Character Datatype
R supports character data types where you have all the alphabets and
special characters. It stores character values or strings. Strings in R can
contain alphabets, numbers, and symbols. The easiest way to denote that a
value is of character type in R is to wrap the value inside single or double
inverted commas.
# A simple R program
# to illustrate character data type
# Assign a character value to char
char = "Geeksforgeeks"
# print the class name of char
print(class(char))
# print the type of char
print(typeof(char))
Find data type of an object
To find the data type of an object you have to use class() function. The
syntax for doing that is you need to pass the object as an argument to the
function class() to find the data type of an object.
Syntax:
class(object)
# A simple R program
# to find data type of an object
# Logical
print(class(TRUE))
# Integer
print(class(3L))
# Numeric
print(class(10.5))
# Complex
print(class(1+2i))
# Character
print(class("12-04-2020"))
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 operands.
In this article, we will see various types of operators in R Programming
language and their usage.
Types of the operator in R language
Arithmetic Operators
Logical Operators
Relational Operators
Assignment Operators
Miscellaneous Operator
Arithmetic Operators
Arithmetic operations simulate various math operations, like addition,
subtraction, multiplication, division, and modulo using the specified operator
between operands, which may be either scalar values, complex numbers, or
vectors. The operations are performed element-wise at the corresponding
positions of the vectors.
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
^ Exponent x^y
Operator Description
Relational Operators
The relational operators carry out comparison operations between the
corresponding elements of the operands. Returns a boolean TRUE value if
the first operand satisfies the relation compared to the second. A TRUE
value is always considered to be greater than the FALSE.
== Equal x == y
!= Not equal x != y
Assignment Operators
Assignment operators are used to assigning values to various data objects in
R. The objects may be integers, vectors, or functions. These values are then
stored by the assigned variable names. There are two kinds of assignment
operators: Left and Right
my_var <- 3
my_var <<- 3
3 -> my_var
3 ->> my_var
Miscellaneous Operators
These are the mixed operators that simulate the printing of sequences and
assignment of vectors, either left or right-handed.
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 which are used to store the
tabular data. Data frames can also be interpreted as matrices where each
column of a matrix can be of the different data types. DataFrame is made up
of three principal components, the data, rows, and columns.
R – Data Frames
Data Frames can have different types of data inside it. While the first column
can be character, the second and third can be numeric or logical. However,
each column should have the same type of data.
Use the data.frame() function to create a data frame:
Example
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame
summary(Data_Frame)
apply() function
The apply() function lets us apply a function to the rows or columns of a
matrix or data frame. This function takes matrix or data frame as an
argument along with function and whether it has to be applied by row or
column and returns the result in the form of a vector or array or list of values
obtained.
syntax: lapply( x, fun )
Parameters:
x: determines the input vector or an object.
fun: determines the function that is to be applied to input data.
Plot
The plot() function is used to draw points (markers) in a diagram.
At its simplest, you can use the plot() function to plot two numbers against
each other:
Example
Draw one point in the diagram, at position (1) and position (3):
plot(1, 3)
Example
Draw two points in the diagram, one at position (1, 3) and one in position (8,
10):
plot(c(1, 8), c(3, 10))
Multiple Points
You can plot as many points as you like, just make sure you have the same
number of points in both axis:
Example
plot(c(1, 2, 3, 4, 5), c(3, 7, 8, 9, 12))
Example
x <- c(1, 2, 3, 4, 5)
y <- c(3, 7, 8, 9, 12)
plot(x, y)
Sequences of Points
If you want to draw dots in a sequence, on both the x-axis and the y-axis,
use the : operator:
Example
plot(1:10)
Draw a Line
The plot() function also takes a type parameter with the value l to draw a
line to connect all the points in the diagram:
Example
plot(1:10, type="l")
Plot Labels
The plot() function also accept other parameters, such
as main, xlab and ylab if you want to customize the graph with a main title and
different labels for the x and y-axis:
Example
plot(1:10, main="My Graph", xlab="The x-axis", ylab="The y axis")
Scatter Plots
You learned from the Plot chapter that the plot() function is used to plot
numbers against each other.
A "scatter plot" is a type of plot used to display the relationship between two
numerical variables, and plots one dot for each observation.
It needs two vectors of same length, one for the x-axis (horizontal) and one
for the y-axis (vertical):
Example
x <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)
plot(x, y)
Pie Charts
A pie chart is a circular graphical view of data.
Example
# Create a vector of pies
x <- c(10,20,30,40)
Example
# x-axis values
x <- c("A", "B", "C", "D")
# y-axis values
y <- c(2, 4, 6, 8)
barplot(y, names.arg = x)
For more programs in R :
https://www.w3schools.com/r/