R Programming First Unit
R Programming First Unit
Module - 1
Introduction to R programming
R is a programming language and software environment for statistical analysis, graphics
representation and reporting.
R is an open-source programming language that is widely used as a statistical software and
data analysis tool. R is an open-source language and it is available for free for everyone to use for
statistical and graphical purposes.R is an interpreted language that supports both procedural
programming and object-oriented programming.
R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New
Zealand, and is currently developed by the R Development Core Team. R is freely available under the
GNU General Public License, and pre-compiled binary versions are provided for various operating
systems like Linux, Windows and Mac. This programming language was named R, based on the first
letter of first name of the two R authors (Robert Gentleman and Ross Ihaka), and partly a play on the
name of the Bell Labs Language S.It also combines with lexical scoping semantics inspired by
Scheme. Moreover, the project conceives in 1992, with an initial version released in 1995 and a
stable beta version in 2000.
R programming is used as a leading tool for machine learning, statistics, and data analysis.
Objects, functions, and packages can easily be created by R.
It‟s a platform-independent language. This means it can be applied to all operating system.
It‟s an open-source free language. That means anyone can install it in any organization without
purchasing a license.
R programming language is not only a statistic package but also allows us to integrate with other
languages (C, C++). Thus, you can easily interact with many data sources and statistical
packages.
The R programming language has a vast community of users and it‟s growing day by day.
R is currently one of the most requested programming languages in the Data Science job market
that makes it the hottest trend nowadays.
a)Statistical Features of R:
Basic Statistics: The most common basic statistics terms are the mean, mode, and median. These
are all known as “Measures of Central Tendency.” So using the R language we can measure
central tendency very easily.
Static graphics: R is rich with facilities for creating and developing interesting static graphics. R
contains functionality for many plot types including graphic maps, mosaic plots, biplots, and the
list goes on.
Probability distributions: Probability distributions play a vital role in statistics and by using R
we can easily handle various types of probability distribution such as Binomial Distribution,
Normal Distribution, Chi-squared Distribution and many more.
Data analysis: It provides a large, coherent and integrated collection of tools for data analysis.
b) Programming Features of R:
R Packages: One of the major features of R is it has a wide availability of libraries. R has
CRAN(Comprehensive R Archive Network), which is a repository holding more than 10, 0000
packages.
Distributed Computing: Distributed computing is a model in which components of a software
system are shared among multiple computers to improve efficiency and performance. Two new
packages ddR and multidplyr used for distributed programming in R were released in
November 2015.
Programming in R:
Since R is much similar to other widely used languages syntactically, it is easier to code and
learn in R. Programs can be written in R in any of the widely used IDE like R Studio, Rattle, Tinn-
R, etc. After writing the program save the file with the extension .r. To run the program use the
following command on the command line:
Sample Program
R file_name.r
# Below line will print "Welcome to GSCH!"
print("Welcome to GSCH!")
Output
Welcome to GSCH!
Advantages of R:
R is the most comprehensive statistical analysis package. As new technology and concepts often
appear first in R.
As R programming language is an open source. Thus, you can run R anywhere and at any time.
R programming language is suitable for GNU/Linux and Windows operating system.
R programming is cross-platform which runs on any operating system.
In R, everyone is welcome to provide new packages, bug fixes, and code enhancements.
Disadvantages of R:
In the R programming language, the standard of some packages is less than perfect.
Applications of R:
We use R for Data Science. It gives us a broad variety of libraries related to statistics. It also
provides the environment for statistical computing and design.
R is used by many quantitative analysts as its programming tool. Thus, it helps in data importing
and cleaning.
R is the most prevalent language. So many data analysts and research programmers use it. Hence,
it is used as a fundamental tool for finance.
Tech giants like Google, Facebook, bing, Twitter, Accenture, Wipro and many more using R
nowadays.
Installation
R can be downloaded from related mirror sites because it is open source software. You should pick
your nearest and freely available softwares.
Variable Assignment
We assign values to variables with the assignment operator "=". Just typing the variable by itself at
the prompt will print out the value. We should note that another form of assignment operator "<-" is
also in use.
>x=1
>x
[1] 1
Functions
R functions are invoked by its name, then followed by the parenthesis, and zero or more arguments.
The following apply the function c to combine three numeric values into a vector.
> c(1, 2, 3)
[1] 1 2 3
Comments
All text after the pound sign "#" within the same line is considered a comment.
>1+1 # this is a comment
[1] 2
Extension Package
Sometimes we need additional functionality beyond those offered by the core R library. In order to
install an extension package, you should invoke the install.packages function at the prompt and
follow the instruction.
> install.packages()
Getting Help
R provides extensive documentation. For example, entering ?c or help(c) at the prompt gives
documentation of the function c in R. Please give it a try.
> help(c)
R Data types
R Data types are used in computer programming to specify the kind of data that can be stored
in a variable. For effective memory consumption and precise computation, the right data type must
be selected. Each R data type has its own set of regulations and restrictions.
Each variable in R has an associated data type. Each R-Data Type requires different amounts of
memory and has some specific operations which can be performed over it. R Programming
language has the following basic R-data types and the following table shows the data type and the
values that each data type can take
Basic Data
Values Examples
Types
“a”, “b”, “c”, …, “@”, “#”, “$”, …., “1”, "character_value <- "Hello
Character
“2”, …etc Geeks"
Numerics in R
Decimal values are called numerics in R. It is the default R data type for numbers in R. If you
assign a decimal value to a variable x as follows, x will be of numeric type. Real numbers with a
decimal point are represented using this data type in R. it uses a format for double-precision
floating-point numbers to represent numerical values.
It is the default computational data type. If we assign a decimal value to a variable x as follows, x will
be of numeric type.
Sample Program
x = 10.5 # assign a decimal value
x # print the value of x
[1] 10.5
class(x) # print the class name of x
[1] "numeric"
Furthermore, even if we assign an integer to a variable k, it is still being saved as a numeric value.
k=1
k # print the value of k
[1] 1
Integer
In order to create an integer variable in R, we invoke the integer function. We can be assured
that y is indeed an integer by applying the is.integer function.
Sample Program
y = as.integer(3)
y # print the value of y
[1] 3
is.integer(y) # is y an integer?
[1] TRUE
Complex
Sample Program
z = 1 + 2i # create a complex number
z # print the value of z
[1] 1+2i
class(z) # print the class name of z
[1] "complex"
Logical
Character
A character object is used to represent string values in R. We convert objects into character values
with the as.character() function:
Sample Program
x = as.character(3.14)
x # print the character string
[1] "3.14"
class(x) # print the class name of x
[1] "character"
Operators
Operators are used in R to perform various operations on variables and values. Among the
most commonly used ones are arithmetic and assignment 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.
Arithmetic Operators
Assignment Operators
Logical Operators
Relational Operators
Miscellaneous Operator
Arithmetic Operators
Arithmetic operations in R 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 R operators are performed element-wise at
the corresponding positions of the vectors.
R supports the following arithmetic operators:
Sample Program
# R program to illustrate
# the use of Arithmetic operators
a <- 2
b <- 3
Output
Addition of operators : 5
Subtraction of operators : -1
Multiplication of operators : 6
Division of operators : 0.6666667
Modulo of operators : 2
Power operators : 8
Assignment Operators
Assignment operators in R 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.
R uses the following assignment operators:
# R program to illustrate
# the use of Assignment operators
vec1 <- c(2:5)
c(2:5) ->> vec2
vec3 <<- c(2:5)
vec4 = c(2:5)
c(2:5) -> vec5
Output
Assign 1 : 2 3 4 5
Assign 2 : 2 3 4 5
Assign 3 : 2 3 4 5
Assign 4 : 2 3 4 5
Assign 5 : 2 3 4 5
Logical Operators
Logical operations in R simulate element-wise decision operations, based on the specified operator
between the operands, which are then evaluated to either a True or False boolean value. Any non-
zero integer value is considered as a TRUE value, be it a complex or real number.
Element-wise Logical AND operator (&)
Returns True if both the operands are True.
Output : FALSE
Compares just the first elements of both the lists.
Output : TRUE
Relational Operators
The relational operators in R 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.
Less than (<)
Returns TRUE if the corresponding element of the first operand is less than that of the second
operand. Else returns FALSE.
# R program to illustrate
# the use of Relational operators
vec1 <- c(0, 2)
vec2 <- c(2, 3)
# Performing operations on Operands
cat ("Vector1 less than Vector2 :", vec1 < vec2, "\n")
cat ("Vector1 less than equal to Vector2 :", vec1 <= vec2, "\n")
cat ("Vector1 greater than Vector2 :", vec1 > vec2, "\n")
cat ("Vector1 greater than equal to Vector2 :", vec1 >= vec2, "\n")
cat ("Vector1 not equal to Vector2 :", vec1 != vec2, "\n")
Output
Vector1 less than Vector2 : TRUE TRUE
Vector1 less than equal to Vector2 : TRUE TRUE
Vector1 greater than Vector2 : FALSE FALSE
Vector1 greater than equal to Vector2 : FALSE FALSE
Vector1 not equal to Vector2 : TRUE TRUE
Miscellaneous Operators
These operators are used to for specific purpose and not general mathematical or logical computation.
Vectors
R vectors are the same as the arrays in C language which are used to hold multiple data values
of the same type. One major key point is that in R the indexing of the vector will start from „1‟ and not
from „0‟. We can create numeric vectors and character vectors as well.
Types of R vectors
Vectors are of different types which are used in R. Following are some of the types of vectors:
Numeric vectors: Numeric vectors are those which contain numeric values such as integer,
float, etc.
# R program to create numeric Vectors
Output
[1] "double"
[1] "integer"
Character vectors: Character vectors in R contain alphanumeric values and special characters.
# R program to create Character Vectors
Logical vectors: Logical vectors in R contain Boolean values such as TRUE, FALSE and NA for
Null values.
# R program to create Logical Vectors
Creating a vector
There are different ways of creating R vectors. Generally, we use „c‟ to combine different elements
together.
using c function 61 4 21 67 89 2
using seq() function 1 3.25 5.5 7.75 10
using colon 2 3 4 5 6 7
Length of R vector
# Create a numeric vector
x <- c(1, 2, 3, 4, 5)
length(x)
# Create a character vector
y <- c("apple", "banana", "cherry")
> length(x)
[1] 5
> length(y)
[1] 3
> length(z)
[1] 4
Note: Vectors in R are 1 based indexing unlike the normal C, python, etc format.
Modifying a R vector
Modification of a Vector is the process of applying some operation on an individual
element of a vector to change its value in the vector. There are different ways through which we can
modify a vector:
# Creating a vector
X<- c(2, 7, 9, 7, 8, 2)
# Modify by specifying
# the position or elements.
X<- X[c(3, 2, 1)]
cat('combine() function', X)
Output:
subscript operator 2 9 1 7 8 2
Logical indexing 0 0 0 0 0 2
combine() function 0 0 0
Deleting a R vector
Deletion of a Vector is the process of deleting all of the elements of the vector. This can be done by
assigning it to a NULL value.
# Creating a Vector
M<- c(8, 10, 2, 5)
# Creation of Vector
X<- c(8, 2, 7, 1, 11, 2)
ascending order 1 2 2 7 8 11
descending order 11 8 7 2 2 1
R – Matrices
Matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know
rows are the ones that run horizontally and columns are the ones that run vertically. In R
programming, matrices are two-dimensional, homogeneous data structures. These are some examples
of matrices.
Creating a Matrix
To create a matrix in R you need to use the function called matrix(). The arguments to this matrix() are
the set of elements in the vector. You have to pass how many numbers of rows and how many
numbers of columns you want to have in your matrix.
Output
The 3x3 matrix:
cde
a123
b456
c789
Parameters:
k: the constant
m: no of rows
n: no of columns
# R program to illustrate
# special matrices
print(matrix(5, 3, 3))
Output:
Diagonal matrix:
A diagonal matrix is a matrix in which the entries outside the main diagonal are all zero. To create
such a R matrix the syntax is given below:
Syntax: diag(k, m, n)
Parameters:
k: the constants/array
m: no of rows
n: no of columns
Example:
# R program to illustrate
# special matrices
R – Array
Arrays are essential data storage structures defined by a fixed number of dimensions. Arrays
are used for the allocation of space at contiguous memory locations. Uni-dimensional arrays are called
vectors with the length being their only dimension. Two-dimensional arrays are called matrices,
consisting of fixed numbers of rows and columns. Arrays consist of all elements of the same data type.
Vectors are supplied as input to the function and then create an array based on the number of
dimensions.
Creating an Array
An array in R can be created with the use of array() function. List of elements is passed to the
array() functions along with the dimensions as required.
Syntax:
where,
nrow : Number of rows
ncol : Number of columns
nmat : Number of matrices of dimensions nrow * ncol
dimnames : Default value = NULL.
Otherwise, a list has to be specified which has a name for each component of the dimension.
Each component is either a null or a vector of length equal to the dim value of that corresponding
dimension.
Uni-Dimensional Array
A vector is a uni-dimensional array, which is specified by a single dimension, length. A Vector
can be created using „c()„ function. A list of values is passed to the c() function to create a vector.
Example:
Output:
[1] 1 2 3 4 5 6 7 8 9
Length of vector : 9
Multi-Dimensional Array
A two-dimensional matrix is an array specified by a fixed number of rows and columns, each
containing the same data type. A matrix is created by using array() function to which the values and
the dimensions are passed.
Example:
print(arr)
Output:
Naming of Arrays
The row names, column names and matrices names are specified as a vector of the number of rows,
number of columns and number of matrices respectively. By default, the rows, columns and matrices
are named by their index values.
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3")
mat_names <- c("Mat1", "Mat2")
arr = array(2:14, dim = c(2, 3, 2), # the naming of the various elements
dimnames = list(row_names, # is specified in a list
col_names, mat_names)) print (arr) # fed to the function
Output:
Mat1
col1 col2 col3
row1 2 4 6
row2 3 5 7
Mat2
col1 col2 col3
row1 8 10 12
row2 9 11 13
R – Lists
A list in R is a generic object consisting of an ordered collection of objects. Lists are one-
dimensional, heterogeneous data structures. The list can be a list of vectors, a list of matrices, a list of
characters and a list of functions, and so on.
A list is a vector but with heterogeneous data elements. A list in R is created with the use of list()
function. R allows accessing elements of an R list with the use of the index value. In R, the indexing of
a list starts with 1 instead of 0 like in other programming languages.
Lists are the R objects which contain elements of different types like − numbers, strings, vectors and
another list inside it. A list can also contain a matrix or a function as its elements. List is created using
list() function.
Creating a List
Following is an example to create a list containing strings, numbers, vectors and a logical values.
# Create a list containing strings, numbers, vectors and a logical
# values.
list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(list_data)
The list elements can be given names and they can be accessed using these names
$`1st_Quarter`
[1] "Jan""Feb""Mar"
$A_Matrix
[,1] [,2] [,3]
[1,] 3 5 -2
[2,] 9 1 8
$A_Inner_list
$A_Inner_list[[1]]
[1] "green"
$A_Inner_list[[2]]
[1] 12.3
# Access the thrid element. As it is also a list, all its elements will be printed.
print(list_data[3])
$`1st_Quarter`
[1] "Jan""Feb""Mar"
$A_Inner_list
$A_Inner_list[[1]]
[1] "green"
$A_Inner_list[[2]]
[1] 12.3
[[1]]
[1] "New element"
$<NA>
NULL
A list can be converted to a vector so that the elements of the vector can be used for further
manipulation. All the arithmetic operations on vectors can be applied after the list is converted into
vectors. To do this conversion, we use the unlist() function. It takes the list as input and produces a
vector
# Create lists.
list1 <- list(1:5)
print(list1)
list2 <-list(10:14)
print(list2)
print(v1)
print(v2)
[[1]]
[1] 1 2 3 4 5
[[1]]
[1] 10 11 12 13 14
[1] 1 2 3 4 5
[1] 10 11 12 13 14
[1] 11 13 15 17 19
Data frame
A data frame is a table or a two-dimensional array-like structure in which each column
contains values of one variable and each row contains one set of values from each column.
Example:
print(friend.data)
friend_id friend_name
1 1 Sachin
2 2 Sourav
3 3 Dravid
4 4 Sehwag
5 5 Dhoni
Get the Structure of the R – Data Frame
One can get the structure of the R data frame using str() function in R. It can display even the internal
structure of large lists which are nested. It provides one-liner output for the basic R objects letting the
user know about the object and its constituents.
Example:
# R program to get the
# structure of the data frame
print(str(friend.data))
'data.frame': 5 obs. of 2 variables:
$ friend_id : int 1 2 3 4 5
$ friend_name: chr "Sachin""Sourav""Dravid""Sehwag" ...
NULL
print(summary(friend.data))
Output:
friend_id friend_name
Min. :1 Length:5
1st Qu.:2 Class :character
Median :3 Mode :character
Mean :3
3rd Qu.:4
Max. :5
Example:
# R program to extract
# data from the data frame
Print(result)
library(dplyr)
# Create a data frame
data <- data.frame(
friend_id = c(1, 2, 3, 4, 5),
friend_name = c("Sachin", "Sourav", "Dravid", "Sehwag", "Dhoni"),
location = c("Kolkata", "Delhi", "Bangalore", "Hyderabad", "Chennai")
)
friend_id friend_name
1 1 Sachin
2 2 Sourav
4 4 Sehwag
5 5 Dhoni
In the above code, we first created a data frame called data with three columns: friend_id,
friend_name, and location. To remove a row with friend_id equal to 3, we used the subset() function
and specified the condition friend_id != 3. This removed the row with friend_id equal to 3.
To remove the location column, we used the select() function and specified -location. The – sign
indicates that we want to remove the location column. The resulting data frame data will have only
two columns: friend_id and friend_name
print(dataframe)
'Anil', 'Suraj',
'Piyush', 'Dheeraj'),
mean(dataframe$team)
Output
NA
'Suraj', 'Piyush',
'Dheeraj'),
mean(dataframe$minor)
Print ( mean)
Output
85.4
Output:
Output is 5
Syntax: is.numeric(x)
Parameters:
x: Object to be checked
Example 1:
# R program to check if
# object is of numeric type
# Calling is.numeric() function
is.numeric(1)
is.numeric(“Program”)
is.numeric(-1.5)
Output:
[1] TRUE
[1] FALSE
[1] TRUE
Special values in R
R comes with some special values. Some of the special values in R are NA, Inf, -Inf, and NaN.
As the name indicates, Missing values are those elements that are not known. NA or NaN are reserved
words that indicate a missing value in R Programming language for q arithmetical operations that are
undefined.
A logical vector is returned by this function that indicates all the NA values present. It returns a
Boolean value. If NA is present in a vector it returns TRUE else FALSE.
is.na(x)
Output:
d <- is.na(x)
x[! d]
Output:
[1] 1 2 3 4
Classes in R Programming
A class is just a blueprint or a sketch of these objects. It represents the set of properties or
methods that are common to all objects of one type.
Unlike most other programming languages, R has a three-class system. These are S3, S4, and
Reference Classes.
A class in R is present in a vector form. Due to this property, the objects can inherit from many
classes, and also we can specify the order of inheritance even for complex classes.
There are 3 types of Class:
S3 Class
S4 Class
Reference Class
S3 Class
S3 is the simplest yet the most popular OOP system and it lacks formal definition and structure. An
object of this type can be created by just adding an attribute to it. Following is an example to make
things more clear:
Example:
Output
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr
In S3 systems, methods don‟t belong to the class. They belong to generic functions. It means that we
can‟t create our own methods here, as we do in other programming languages like C++ or Java. But
we can define what a generic method (for example print) does when applied to our objects.
print(movieList)
Output:
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"
Example: Creating a user-defined print function
Output:
S4 Class
Programmers of other languages like C++, Java might find S3 to be very much different than
their normal idea of classes as it lacks the structure that classes are supposed to provide. S4 is a slight
improvement over S3 as its objects have a proper definition and it gives a proper structure to its
objects.
Example:
library(methods)
# definition of S4 class
# creating an object using new() by passing class name and slot values
movieList <- new("movies", name="Iron man", leadActor = "Robert Downey Jr")
movieList
Output:
Reference Class
Reference Class is an improvement over S4 Class. Here the methods belong to the classes.
These are much similar to object-oriented classes of other languages.
Defining a Reference class is similar to defining S4 classes. We use setRefClass() instead of setClass()
and “fields” instead of “slots”.
Example:
library(methods)
# setRefClass returns a generator
movies <- setRefClass("movies", fields = list(name = "character",
leadActor = "character", rating = "numeric"))
Output:
Reference class object of class "movies"
Field "name":
[1] "Iron Man"
Field "leadActor":
[1] "Robert downey Jr"
Field "rating":
[1] 7
Coercion
When you call a function with an argument of the wrong type, R will try to coerce values to a
different type so that the function will work. There are two types of coercion that occur automatically
in R: coercion with formal objects and coercion with built-in types.
Coercion includes type conversion . Type conversion means change of one type of data into another
type of data. We have to type of coercion occurs :
1. Implicit Coercion
2. Explicit Coercion
Implicit Coercion : When type conversion occurs by itself in R. We input numeric and character data
in an object . R converts numeric data to character data by itself
Implicit coercion occurs when we operate on a vector in a way that is not intended for its type. For
example, if we add 1 to a logical vector, then the logical values are converted to 0s and 1s implicitly,
and 1 is added to each element
Explicit Coercion :
In explicit coercion , we can change one data type to another data type by applying function.
We create an object “x” which stores integer values from 1 to 6.
x<-0:6
class(x)
z<-as.numeric(x)
class(z)
Basic plots
Basic plots in R. R has a number of built-in tools for basic graph types such as histograms,
scatter plots, bar charts, boxplots and much more. Rather than going through all of different types, we
will focus on plot() , a generic function for plotting x-y data.
Plot
The plot() function is used to draw points (markers) in a diagram.
The function takes parameters for specifying points in the diagram.
One-dimensional Plotting: In one-dimensional plotting, we plot one variable at a time. For example,
we may plot a variable with the number of times each of its values occurred in the entire dataset
(frequency). So, it is not compared to any other variable of the dataset.
Two-dimensional Plotting
In two-dimensional plotting, we visualize and compare one variable with respect to the other. For
example, in a dataset of Air Quality measures, we would like to compare how the AQI varies with the
temperature at a particular place. So, temperature and AQI are two different variables and we wish to
see how one changes with respect to the other. These are the 3 major kinds of graphs used for such
kinds of analysis
R - Bar Charts
A bar chart represents data in rectangular bars with length of the bar proportional to the value of the
variable. R uses the function barplot() to create bar charts. R can draw both vertical and Horizontal
bars in the bar chart. In bar chart each of the bars can be given different colors.
Syntax
The basic syntax to create a bar-chart in R is −
barplot(H,xlab,ylab,main, names.arg,col)
Following is the description of the parameters used −
Example
A simple bar chart is created using just the input vector and the name of each bar.
# Create the data for the chart
A <- c(17, 2, 8, 13, 1, 22)
B <- c("Jan", "feb", "Mar", "Apr", "May", "Jun")
Histograms in R language
A histogram contains a rectangular area to display the statistical information which is
proportional to the frequency of a variable and its width in successive numerical intervals. A graphical
representation that manages a group of data points into different specified ranges. It has a special
feature that shows no gaps between the bars and is similar to a vertical bar graph.
R – Histograms
We can create histograms in R Programming Language using the hist() function.
Parameters:
Example:
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39)
Boxplots in R Language
A box graph is a chart that is used to display information in the form of distribution by drawing
boxplots for each of them. This distribution of data is based on five sets (minimum, first quartile,
median, third quartile, and maximum).
Boxplots in R Programming Language
Boxplots are created in R by using the boxplot() function.
Parameters:
Creating a Dataset
We use the data set “mtcars”.
Let‟s look at the columns “mpg” and “cyl” in mtcars.
input <- mtcars[, c('mpg', 'cyl')]
print(head(input))
Output:
mpg cyl
Mazda RX4 21.0 6
Mazda RX4 Wag 21.0 6
Datsun 710 22.8 4
Hornet 4 Drive 21.4 6
Hornet Sportabout 18.7 8
Valiant 18.1 6
Creating the Boxplot
Creating the Boxplot graph.
data(mtcars)