SlideShare une entreprise Scribd logo
1  sur  44
R language tutorial
UNIT-1
Introduction:
 R is a programming language and software environment for statistical analysis,
graphics representation and reporting. 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. R is freely available under the GNU General Public License
Features of R
 R is a well-developed, simple and effective programming language which
includes conditionals, loops, user defined recursive functions and input and
output facilities.
 R has an effective data handling and storage facility,
 R provides a suite of operators for calculations on arrays, lists, vectors and
matrices.
 R provides graphical facilities for data analysis and display either directly at the
computer or printing at the papers.
Basic Syntax
 First method
 a <- "Hello, World!"
 print ( a) //enter run in scripting window
 2nd method
 a = "Hello, World!"
 a //in console window
Output
Hello, World!
Data type
 Like other programming languages C and java, in R the variables are not
declared as some data type. The variables are assigned with R-Objects and the
data type of the R-object becomes the data type of the variable.
 It has a dynamic datatype means it takes datatype as values are to be stored.
Different types of data types
 Logical TRUE, FALSE
 Numeric 12.3, 5, 999
 Integer 2L, 34L, 0L
 Complex 3 + 2i
 Character'a' , '"good",
Example:
 (1) a=3
 a
 Output:=3
 If we want to see the data type of this variable then use the syntax:
 Class(a)
 Output: “numeric”
 (2) a=“hello”
 A
 Output: “hello”
 Class(a)
 Output: “character”
 (3) A=true
 A
 Output: true
 (4) b=10+i6
 B
 Output: 10+ib
 Class(b)
 Output: complex
Data structure OR Object in R
 There are many types of R-objects. The frequently used ones are −
• Vectors
• Lists
• Matrices
• Arrays
• Factors
• Data Frames
Vectors
 Vectors are the most basic R data objects and there are six types of atomic
vectors. They are logical, integer, double, complex, character.
 There are two type of vector:
 Single Element Vector
 Multiple elements vector
# Atomic vector of type character.
print(“XYZ");
# Atomic vector of type double.
print(17.6)
# Atomic vector of type integer.
print(76L)
# Atomic vector of type logical.
print(TRUE)
# Atomic vector of type complex.
print(8+9i)
Single element vector
Multiple element vector
When you want to create vector with more than one element, you should use c()
function which means to combine the elements into a vector.
fruit <- c(‘‘mango’’,’’orange’’,”apple")
print(fruit)
Output: (‘‘mango’’,’’orange’’,”apple")
# Get the class of the vector.
print(class(fruit))
Output: “character”
Using colon operator with numeric
data
 # Creating a sequence from 9 to 18.
 a <- 9:18
 print(a)
 Output: 9,10,11,12,13,14,15,16,17,18
 # Creating a sequence from 7.6 to 11.6.
 a <- 7.6:11.6
 print(a)
 Output: 7.6 8.6 9.6 10.6 11.6
 # If the final element specified does not belong to the sequence then it is
discarded.
 a <- 4.8:9.4
 print(a)
 Output:
 4.8 5.8 6.8 7.8 8.8 9.8 10.8
Using sequence (Seq.) operator
 # Create vector with elements from 7to 11 incrementing by 0.5.
 print(seq(7, 11, by = 0.5))
 Output: 7,7.5,8,8.5,9,9.5,10,10.5,11
Using the c() function
 If we use the different type of values in C then the logical and numeric
values are converted to characters.
 s <- c(‘mango','red’,9,false)
 print(s)
 Output: ‘mango','red’,’9’,’false’
Accessing Vector Elements by position
 Elements of a Vector are accessed using indexing. The [ ] brackets are used for
indexing. Indexing starts with position 1. Giving a negative value in the index
drops that element from result. TRUE, FALSE or 0 and 1 can also be used for
indexing.
# Accessing vector elements using position.
t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
l <- t[c(3,4,6)]
print(l)
Output: "Tue" “Wed” "Fri"
# Accessing vector elements using logical indexing.
v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]
print(v)
# Accessing vector elements using negative indexing.
x <- t[c(-2,-5)]
print(x)
# Accessing vector elements using 0/1 indexing.
y <- t[c(0,0,0,0,0,0,1)]
print(y)
Operators in R
 An operator is a symbol that tells the compiler to perform specific mathematical
or logical manipulations. R language is rich in built-in operators and provides
following types of operators.
 Types of Operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators

Arithmetic Operators
 +----
 v <- c( 2,5.5,6)
 t <- c(8, 3, 4)
 print(v+t)
 Output: 10.0 8.5 10.0
 Same as we can use -,*, / as well.
 %% --- Give the remainder of the first vector with the second
 Example: v <- c( 2,5.5,6)
 t <- c(8, 3, 4)
 print(v%%t)
 Output: 2.0 2.5 2.0
 %/%--- The result of division of first vector with second (quotient)
 v <- c( 2,5.5,6)
 t <- c(8, 3, 4)
 print(v%/%t)
 Output: 0 1 1
 ^----The first vector raised to the exponent of second vector
 v <- c( 2,5.5,6)
 t <- c(8, 3, 4)
 print(v^t)
 Output: 256.000 166.375 1296.000
Relational Operators
 In this operator each element of the first vector is compared with the
corresponding element of the second vector. The result of comparison is a
Boolean value.
 > Checks if each element of the first vector is greater than the
corresponding element of the second vector.
v <- c(2,5.5,6,9)
 t <- c(8,2.5,14,9)
 print(v>t)
 FALSE TRUE FALSE FALSE
 < Checks if each element of the first vector is less than the corresponding
element of the second vector.
 v <- c(2,5.5,6,9)
 t <- c(8,2.5,14,9)
 print(v < t)
 TRUE FALSE TRUE FALSE
 == Checks if each element of the first vector is equal to the corresponding
element of the second vector.
<= Checks if each element of the first vector is less than or equal to the
corresponding element of the second vector.
>= Checks if each element of the first vector is greater than or equal to the
corresponding element of the second vector.
!= Checks if each element of the first vector is unequal to the
corresponding element of the second vector.
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v!=t)
Logical Operators
 It is applicable only to vectors of type logical, numeric or complex. All
numbers greater than 1 are considered as logical value TRUE.
 Each element of the first vector is compared with the corresponding element
of the second vector. The result of comparison is a Boolean value.
AND, OR, NOT
& --- It is called Element-wise Logical AND operator. It combines each element
of the first vector with the corresponding element of the second vector and gives
a output TRUE if both the elements are TRUE.
v <- c(0,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
Print (v&t)
Output: FALSE TRUE FALSE TRUE
OR
 |--- It is called Element-wise Logical OR operator. It combines each element
of the first vector with the corresponding element of the second vector and
gives a output TRUE if one the elements is TRUE.
 Example:
 v <- c(3,0,TRUE,4+2i)
 t <- c(0,1,FALSE,2+3i)
 print(v|t)
 Output:
 TRUE TRUE TRUE TRUE
 !--- It is called Logical NOT operator. Takes each element of the vector and
gives the opposite logical value.
 v <- c(3,0,TRUE,2+2i)
 print(!v)
 Output: FALSE TRUE FALSE FALSE
 && --- Called Logical AND operator. Takes first element of both the vectors and
gives the TRUE only if both are TRUE.
 Example:
 v <- c(3,0,TRUE,2+2i)
 t <- c(1,3,TRUE,2+3i)
 print(v&&t)
 Output: TRUE
 v <- c(0,3,TRUE,2+2i)
 t <- c(1,0,TRUE,2+3i)
 print(v&&t)
 Output: false
 ||--- Called Logical OR operator. Takes first element of both the vectors and
gives the TRUE if one of them is TRUE.
 v <- c(0,0,TRUE,2+2i)
 t <- c(0,3,TRUE,2+3i)
 print(v||t)
 Output: FALSE
Assignment Operators
 These operators are used to assign values to vectors.
 <− or = or <<− all are known as left assignment.
 -> or ->> all are known as right assignment.
Vector arithmetic
 Two vectors of same length can be added, subtracted, multiplied or divided
giving the result as a vector output.
 # Create two vectors.
 v1 <- c(3,8,4,5,0,11)
 v2 <- c(4,11,0,8,1,2)
 # Vector addition.
 add.result <- v1+v2
 print(add.result)
NOTE:
 Same as we can apply subtraction, multiplication and division on vectors.
 If we apply arithmetic operations to two vectors of unequal length, then the
elements of the shorter vector are recycled to complete the operations.
 v1 <- c(3,8,4,5,0,11)
 v2 <- c(4,11)
 S <- v1+v2
Elements in a vector can be sorted using
the sort() function.
 v <- c(3,8,4,5,0,11, -9, 304)
 s <- sort(v)
 print(s)
 -9 0 3 4 5 8 11 304
# Sort the elements in the reverse order.
 r <- sort(v, decreasing = TRUE)
 print(r)
 Output: 304 11 8 5 4 3 0 -9
Lists
 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.
 Example:
a = list("orange", "red", c(23,32,61), FALSE, 71.83, 819.7)
a
[[1]]
[1] "orange"
[[2]]
[1] "red"
[[3]]
[1] 23 32 61
[[4]]
[1] FALSE
[[5]]
[1] 71.83
[[6]]
[1] 819.7
Naming List Elements
 The list elements can be given names and they can be accessed using these
names.
# Create a list containing a vector, a matrix and a list.
a <- list(c("Jan","Feb","Mar“,”April”, “May”,”June”), matrix(c(5,5,8,-
2,-9), nrow = 2), list("green",17.3))
# Give names to the elements in the list.
names(a) <- c(“Two Quarter", "A_Matrix", "A list")
# Show the list.
print(a)
 a = list(c("Jan","Feb","Mar“,”April”, “May”,”June”), matrix(c(5,5,8,-2,-9),
nrow = 2), list("green",12.3))
 names(a) = c(“Two Quarter", "A_Matrix", "A list")
 a
Accessing List Elements
 Elements of the list can be accessed by the index of the element in the list. In
case of named lists it can also be accessed using the names.
 print(a [1])
 print(a [3])
 # Access the list element using the name of the element.
 print(a $A_Matrix)
Merging Lists
 You can merge many lists into one list by placing all the lists inside one list()
function.
 # Create two lists.
 list1 <- list(3,3,4,5)
 list2 <- list("Sunday","Monday","Tuesday")
 # Merge the two lists.
 merged.list <- c(list1,list2)
 # Print the merged list.
 print(merged.list)
Converting List to Vector
 A list can be converted to a vector so that the elements of the vector. 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.
 # Create lists.
 list1 <- list(1:5)
 print(list1)
 list2 <-list(10:14)
 print(list2)
 # Convert the lists to vectors.
 v1 <- unlist(list1)
 v2 <- unlist(list2)
 print(v1)
 print(v2)
 # Now add the vectors
 result <- v1+v2
 print(result)

Contenu connexe

Similaire à R language tutorial.pptx

R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptxkalai75
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functionsNIKET CHAURASIA
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in Rnaroranisha
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 
Day 1b R structures objects.pptx
Day 1b   R structures   objects.pptxDay 1b   R structures   objects.pptx
Day 1b R structures objects.pptxAdrien Melquiond
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming languageMegha V
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data framesExternalEvents
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsMegha V
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data framesFAO
 

Similaire à R language tutorial.pptx (20)

R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
R training2
R training2R training2
R training2
 
Data Types of R.pptx
Data Types of R.pptxData Types of R.pptx
Data Types of R.pptx
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
2 data structure in R
2 data structure in R2 data structure in R
2 data structure in R
 
R programming
R programmingR programming
R programming
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
Day 1b R structures objects.pptx
Day 1b   R structures   objects.pptxDay 1b   R structures   objects.pptx
Day 1b R structures objects.pptx
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
 
R language introduction
R language introductionR language introduction
R language introduction
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Introduction2R
Introduction2RIntroduction2R
Introduction2R
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
Matlab1
Matlab1Matlab1
Matlab1
 

Dernier

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Dernier (20)

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

R language tutorial.pptx

  • 2. Introduction:  R is a programming language and software environment for statistical analysis, graphics representation and reporting. 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. R is freely available under the GNU General Public License
  • 3. Features of R  R is a well-developed, simple and effective programming language which includes conditionals, loops, user defined recursive functions and input and output facilities.  R has an effective data handling and storage facility,  R provides a suite of operators for calculations on arrays, lists, vectors and matrices.  R provides graphical facilities for data analysis and display either directly at the computer or printing at the papers.
  • 4. Basic Syntax  First method  a <- "Hello, World!"  print ( a) //enter run in scripting window  2nd method  a = "Hello, World!"  a //in console window Output Hello, World!
  • 5. Data type  Like other programming languages C and java, in R the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable.  It has a dynamic datatype means it takes datatype as values are to be stored.
  • 6. Different types of data types  Logical TRUE, FALSE  Numeric 12.3, 5, 999  Integer 2L, 34L, 0L  Complex 3 + 2i  Character'a' , '"good",
  • 7. Example:  (1) a=3  a  Output:=3  If we want to see the data type of this variable then use the syntax:  Class(a)  Output: “numeric”  (2) a=“hello”  A  Output: “hello”  Class(a)  Output: “character”
  • 8.  (3) A=true  A  Output: true  (4) b=10+i6  B  Output: 10+ib  Class(b)  Output: complex
  • 9. Data structure OR Object in R  There are many types of R-objects. The frequently used ones are − • Vectors • Lists • Matrices • Arrays • Factors • Data Frames
  • 10. Vectors  Vectors are the most basic R data objects and there are six types of atomic vectors. They are logical, integer, double, complex, character.  There are two type of vector:  Single Element Vector  Multiple elements vector
  • 11. # Atomic vector of type character. print(“XYZ"); # Atomic vector of type double. print(17.6) # Atomic vector of type integer. print(76L) # Atomic vector of type logical. print(TRUE) # Atomic vector of type complex. print(8+9i) Single element vector
  • 12. Multiple element vector When you want to create vector with more than one element, you should use c() function which means to combine the elements into a vector. fruit <- c(‘‘mango’’,’’orange’’,”apple") print(fruit) Output: (‘‘mango’’,’’orange’’,”apple") # Get the class of the vector. print(class(fruit)) Output: “character”
  • 13. Using colon operator with numeric data  # Creating a sequence from 9 to 18.  a <- 9:18  print(a)  Output: 9,10,11,12,13,14,15,16,17,18
  • 14.  # Creating a sequence from 7.6 to 11.6.  a <- 7.6:11.6  print(a)  Output: 7.6 8.6 9.6 10.6 11.6  # If the final element specified does not belong to the sequence then it is discarded.  a <- 4.8:9.4  print(a)  Output:  4.8 5.8 6.8 7.8 8.8 9.8 10.8
  • 15. Using sequence (Seq.) operator  # Create vector with elements from 7to 11 incrementing by 0.5.  print(seq(7, 11, by = 0.5))  Output: 7,7.5,8,8.5,9,9.5,10,10.5,11
  • 16. Using the c() function  If we use the different type of values in C then the logical and numeric values are converted to characters.  s <- c(‘mango','red’,9,false)  print(s)  Output: ‘mango','red’,’9’,’false’
  • 17. Accessing Vector Elements by position  Elements of a Vector are accessed using indexing. The [ ] brackets are used for indexing. Indexing starts with position 1. Giving a negative value in the index drops that element from result. TRUE, FALSE or 0 and 1 can also be used for indexing.
  • 18. # Accessing vector elements using position. t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat") l <- t[c(3,4,6)] print(l) Output: "Tue" “Wed” "Fri" # Accessing vector elements using logical indexing. v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)] print(v) # Accessing vector elements using negative indexing. x <- t[c(-2,-5)] print(x) # Accessing vector elements using 0/1 indexing. y <- t[c(0,0,0,0,0,0,1)] print(y)
  • 19. Operators in R  An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides following types of operators.  Types of Operators: • Arithmetic Operators • Relational Operators • Logical Operators • Assignment Operators 
  • 20. Arithmetic Operators  +----  v <- c( 2,5.5,6)  t <- c(8, 3, 4)  print(v+t)  Output: 10.0 8.5 10.0  Same as we can use -,*, / as well.  %% --- Give the remainder of the first vector with the second  Example: v <- c( 2,5.5,6)  t <- c(8, 3, 4)  print(v%%t)  Output: 2.0 2.5 2.0
  • 21.  %/%--- The result of division of first vector with second (quotient)  v <- c( 2,5.5,6)  t <- c(8, 3, 4)  print(v%/%t)  Output: 0 1 1  ^----The first vector raised to the exponent of second vector  v <- c( 2,5.5,6)  t <- c(8, 3, 4)  print(v^t)  Output: 256.000 166.375 1296.000
  • 22. Relational Operators  In this operator each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.  > Checks if each element of the first vector is greater than the corresponding element of the second vector. v <- c(2,5.5,6,9)  t <- c(8,2.5,14,9)  print(v>t)  FALSE TRUE FALSE FALSE
  • 23.  < Checks if each element of the first vector is less than the corresponding element of the second vector.  v <- c(2,5.5,6,9)  t <- c(8,2.5,14,9)  print(v < t)  TRUE FALSE TRUE FALSE
  • 24.  == Checks if each element of the first vector is equal to the corresponding element of the second vector. <= Checks if each element of the first vector is less than or equal to the corresponding element of the second vector. >= Checks if each element of the first vector is greater than or equal to the corresponding element of the second vector. != Checks if each element of the first vector is unequal to the corresponding element of the second vector. v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v!=t)
  • 25. Logical Operators  It is applicable only to vectors of type logical, numeric or complex. All numbers greater than 1 are considered as logical value TRUE.  Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.
  • 26. AND, OR, NOT & --- It is called Element-wise Logical AND operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if both the elements are TRUE. v <- c(0,1,TRUE,2+3i) t <- c(4,1,FALSE,2+3i) Print (v&t) Output: FALSE TRUE FALSE TRUE
  • 27. OR  |--- It is called Element-wise Logical OR operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if one the elements is TRUE.  Example:  v <- c(3,0,TRUE,4+2i)  t <- c(0,1,FALSE,2+3i)  print(v|t)  Output:  TRUE TRUE TRUE TRUE
  • 28.  !--- It is called Logical NOT operator. Takes each element of the vector and gives the opposite logical value.  v <- c(3,0,TRUE,2+2i)  print(!v)  Output: FALSE TRUE FALSE FALSE
  • 29.  && --- Called Logical AND operator. Takes first element of both the vectors and gives the TRUE only if both are TRUE.  Example:  v <- c(3,0,TRUE,2+2i)  t <- c(1,3,TRUE,2+3i)  print(v&&t)  Output: TRUE  v <- c(0,3,TRUE,2+2i)  t <- c(1,0,TRUE,2+3i)  print(v&&t)  Output: false
  • 30.  ||--- Called Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE.  v <- c(0,0,TRUE,2+2i)  t <- c(0,3,TRUE,2+3i)  print(v||t)  Output: FALSE
  • 31. Assignment Operators  These operators are used to assign values to vectors.  <− or = or <<− all are known as left assignment.  -> or ->> all are known as right assignment.
  • 32. Vector arithmetic  Two vectors of same length can be added, subtracted, multiplied or divided giving the result as a vector output.  # Create two vectors.  v1 <- c(3,8,4,5,0,11)  v2 <- c(4,11,0,8,1,2)  # Vector addition.  add.result <- v1+v2  print(add.result)
  • 33. NOTE:  Same as we can apply subtraction, multiplication and division on vectors.  If we apply arithmetic operations to two vectors of unequal length, then the elements of the shorter vector are recycled to complete the operations.  v1 <- c(3,8,4,5,0,11)  v2 <- c(4,11)  S <- v1+v2
  • 34. Elements in a vector can be sorted using the sort() function.  v <- c(3,8,4,5,0,11, -9, 304)  s <- sort(v)  print(s)  -9 0 3 4 5 8 11 304 # Sort the elements in the reverse order.  r <- sort(v, decreasing = TRUE)  print(r)  Output: 304 11 8 5 4 3 0 -9
  • 35. Lists  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.  Example:
  • 36. a = list("orange", "red", c(23,32,61), FALSE, 71.83, 819.7) a [[1]] [1] "orange" [[2]] [1] "red" [[3]] [1] 23 32 61 [[4]] [1] FALSE [[5]] [1] 71.83 [[6]] [1] 819.7
  • 37. Naming List Elements  The list elements can be given names and they can be accessed using these names.
  • 38. # Create a list containing a vector, a matrix and a list. a <- list(c("Jan","Feb","Mar“,”April”, “May”,”June”), matrix(c(5,5,8,- 2,-9), nrow = 2), list("green",17.3)) # Give names to the elements in the list. names(a) <- c(“Two Quarter", "A_Matrix", "A list") # Show the list. print(a)
  • 39.  a = list(c("Jan","Feb","Mar“,”April”, “May”,”June”), matrix(c(5,5,8,-2,-9), nrow = 2), list("green",12.3))  names(a) = c(“Two Quarter", "A_Matrix", "A list")  a
  • 40. Accessing List Elements  Elements of the list can be accessed by the index of the element in the list. In case of named lists it can also be accessed using the names.  print(a [1])  print(a [3])  # Access the list element using the name of the element.  print(a $A_Matrix)
  • 41. Merging Lists  You can merge many lists into one list by placing all the lists inside one list() function.  # Create two lists.  list1 <- list(3,3,4,5)  list2 <- list("Sunday","Monday","Tuesday")  # Merge the two lists.  merged.list <- c(list1,list2)  # Print the merged list.  print(merged.list)
  • 42. Converting List to Vector  A list can be converted to a vector so that the elements of the vector. 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.
  • 43.  # Create lists.  list1 <- list(1:5)  print(list1)  list2 <-list(10:14)  print(list2)
  • 44.  # Convert the lists to vectors.  v1 <- unlist(list1)  v2 <- unlist(list2)  print(v1)  print(v2)  # Now add the vectors  result <- v1+v2  print(result)