SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
The R Language
A Hands-on Introduction
Venkatesh-Prasad Ranganath
http://about.me/rvprasad
What is R?
• A dynamical typed programming language
• http://cran.r-project.org/
• Open source and free
• Provides common programming language constructs/features
• Multiple programming paradigms
• Numerous libraries focused on various data-rich topics
• http://cran.r-project.org/web/views/
• Ideal for statistical calculation; lately, the go-to tool for data analysis
• Accompanied by RStudio, a simple and powerful IDE
• http://rstudio.org
Data Types (Modes)
• Numeric
• Character
• Logical (TRUE / FALSE)
• Complex
• Raw (bytes)
Data Structures
• Vectors
• Matrices
• Arrays
• Lists
• Data Frames
• Factors
• Tables
Data Structures: Vectors
• A sequence of objects of the same (atomic) data type
• Creation
• x <- b c [ <- is the assignment operator ]
• y <- seq(5, 9, 2) = c(5, 7, 9)
• y <- 5:7 = c(5, 6, 7) [ m:n is equivalent to seq(m, n, 1) ]
• y <- c(1, 4:6) = c(1, 4, 5, 6) [ no nesting / always flattened ]
• z <- rep(1, 3) = c(1, 1, 1)
Data Structures: Vectors
• Accessing
• x[1] [ 1-based indexing ]
• x[2:3]
• x[c(2,3)] = x[2:3]
• x[-1] [ Negative subscripts imply exclusion ]
• Naming
• names(x) <- [ Makes equivalent to x[1] ]
Data Structures: Vectors
• Operations
• x <- c(5, 6, 7)
• x + 2 = c(7, 8, 9) [ Vectorized operations ]
• x > 5 = c(FALSE, TRUE, TRUE)
• subset(x, x > 5) = c(6, 7)
• which(x > 5) = c(2, 3)
• ifelse(x > 5, NaN, x) = c(5, NaN, NaN)
• sqr <- function (n) { n * n }
• sapply(x,sqr) = c(25 ,36, 49)
• sqr(x) = c(25, 36, 49)
Data Structures: Vectors
• Operations
• x <- c(5, 6, 7)
• any(x > 5) = TRUE [ How about all(x > 5)? ]
• sum(c(1, 2, 3, NA), na.rm = TRUE) = 6 [ Why is na.rm required? ]
• sort(c(7, 6, 5)) = c(5, 6, 7)
• order(c(7, 6, 5)) = ???
• subset(x, x > 5) = c(6, 7)
• head(1:100) = ???
• tail(1:100) = ???
• How is x == c(5, 6, 7) different from identical(x, c(5, 6, 7))?
• Try str(x)
Data Structures: Matrices
• A two dimensional matrix of objects of the same (atomic) data type
• Creation
• y <- matrix(nrow=2, ncol=3) [ empty matrix ]
• y <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2) =
• y <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2, byrow=T) =
• Accessing
• y[1,2] = 2
• y[,2:3] = [ How about y[1,]? ]
• What’s the difference between y[2,] and y[2,, drop=FALSE]?
1 3 5
2 4 6
1 2 3
4 5 6
2 3
5 6
Data Structures: Matrices
• Naming
• rownames() and colnames()
• Operations
• nrow(y) = 2 [ number of rows ]
• ncol(y) = 3 [ number of columns ]
• apply(y, 1, sum) = c(6, 15) [ apply sum to each row ]
• apply(y, 2, sum) = c(5, 7, 9) [ apply sum to each column ]
• t(y) = [ transpose a matrix ]1 4
2 5
3 6
Data Structures: Matrices
• Operations
• rbind(y, c(7, 8, 9)) =
• cbind(y, c(7, 8)) =
• Try str(y)
1 2 3
4 5 6
7 8 9
1 2 3 7
4 5 6 8
Data Structures: Matrices
• What will this yield?
m <- matrix(nrow=4, ncol=4)
m <- ifelse(row(m) == col(m), 1, 0.3)
Data Structures: Lists
• A sequence of objects (of possibly different data types)
• Creation
• k <- list(c(1, 2, 3),
• l <- [ f1 and f2 are tags ]
• Accessing
• k[2:3]
• k[[2]] [ How about k[2]? ]
• l$f1 = c(1, 2, 3) [ Is it same as l[1] or l[[1]]? ]
Data Structures: Lists
• Naming
• names(k) <-
• Operations
• lapply(list(1:2, 9:10), sum) = list(3, 19)
• sapply(list(1:2, 9:10), sum) = c(3, 19)
• l$f1 <- NULL = ???
• str(l) = ???
Data Structures: Data Frames
• A two dimensional matrix of objects where different columns can be of
different types.
• Creation
• x <- data.frame jill
• Accessing
• x$names jill [ How about x[[1]]? ]
• x[1] = ???
• x[c(1,2)] = ???
• x[1,] = ???
• x[,1] = ???
Data Structures: Data Frames
• Naming
• rownames() and colnames()
• Operations
• x[x$age > 5,] = data.frame jill ))
• subset(x, age > 5) = ???
• apply(x, 1, sum) = ???
• y <- data.frame(1:3, 5:7)
• apply(y, 1, mean) = ???
• lapply(y, mean) = ???
• sapply(y, mean) = ???
• Try str(y)
Factors (and Tables)
• Type for categorical/nominal values.
• Example
• xf <- factor(c(1:3, 2, 4:5))
• Try xf and str(xf)
• Operations
• table(xf) = ???
• with(mtcars, split(mpg, cyl)) = ???
• with(mtcars, tapply(mpg, cyl, mean)) = ???
• by(mtcars, mtcars$cyl, function(m) { median(m$mpg) } = ???
• aggregate(mtcars, list(mtcars$cyl), median) = ???
• You can use cut to bin values and create factors. Try it.
Basic Graphs
• with(mtcars, boxplot(mpg))
• hist(mtcars$mpg)
• with(mtcars, plot(hp, mpg))
• dotchart(VADeaths)
• Try plot(aggregate(mtcars, list(mtcars$cyl), median))
You can get the list of datasets via ls package.datasets
Stats 101 using R
• mean
• median
• What about mode?
• fivenum
• quantile
• sd
• var
• cov
• cor
Data Exploration using R
Let’s get out hands dirty!!

Weitere ähnliche Inhalte

Was ist angesagt?

Motivation and Mechanics behind some aspects of Shapeless
Motivation and Mechanics behind some aspects of ShapelessMotivation and Mechanics behind some aspects of Shapeless
Motivation and Mechanics behind some aspects of ShapelessAnatolii Kmetiuk
 
Rattle Graphical Interface for R Language
Rattle Graphical Interface for R LanguageRattle Graphical Interface for R Language
Rattle Graphical Interface for R LanguageMajid Abdollahi
 
Kof2008 Itll
Kof2008 ItllKof2008 Itll
Kof2008 Itllujihisa
 
Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and stringMuntasirMuhit
 
NTCIR11-Math2-PattaniyilN_poster
NTCIR11-Math2-PattaniyilN_posterNTCIR11-Math2-PattaniyilN_poster
NTCIR11-Math2-PattaniyilN_posterNidhin Pattaniyil
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
Purely Functional Data Structures
Purely Functional Data StructuresPurely Functional Data Structures
Purely Functional Data StructuresJean-Baptiste Mazon
 
Data science : R Basics Harvard University
Data science : R Basics Harvard UniversityData science : R Basics Harvard University
Data science : R Basics Harvard UniversityMrMoliya
 
The Very ^ 2 Basics of R
The Very ^ 2 Basics of RThe Very ^ 2 Basics of R
The Very ^ 2 Basics of RWinston Chen
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional ProgrammingYuan Wang
 
Wrokflow programming and provenance query model
Wrokflow programming and provenance query model  Wrokflow programming and provenance query model
Wrokflow programming and provenance query model Rayhan Ferdous
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
Python data structures (Lists and tuples) presentation
Python data structures (Lists and tuples) presentationPython data structures (Lists and tuples) presentation
Python data structures (Lists and tuples) presentationVedaGayathri1
 
R statistics with mongo db
R statistics with mongo dbR statistics with mongo db
R statistics with mongo dbMongoDB
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 

Was ist angesagt? (20)

Motivation and Mechanics behind some aspects of Shapeless
Motivation and Mechanics behind some aspects of ShapelessMotivation and Mechanics behind some aspects of Shapeless
Motivation and Mechanics behind some aspects of Shapeless
 
Rattle Graphical Interface for R Language
Rattle Graphical Interface for R LanguageRattle Graphical Interface for R Language
Rattle Graphical Interface for R Language
 
R programmingmilano
R programmingmilanoR programmingmilano
R programmingmilano
 
Kof2008 Itll
Kof2008 ItllKof2008 Itll
Kof2008 Itll
 
702 present
702 present702 present
702 present
 
Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and string
 
NTCIR11-Math2-PattaniyilN_poster
NTCIR11-Math2-PattaniyilN_posterNTCIR11-Math2-PattaniyilN_poster
NTCIR11-Math2-PattaniyilN_poster
 
R program
R programR program
R program
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Purely Functional Data Structures
Purely Functional Data StructuresPurely Functional Data Structures
Purely Functional Data Structures
 
Data science : R Basics Harvard University
Data science : R Basics Harvard UniversityData science : R Basics Harvard University
Data science : R Basics Harvard University
 
Type-Aware Entity Retrieval
Type-Aware Entity RetrievalType-Aware Entity Retrieval
Type-Aware Entity Retrieval
 
The Very ^ 2 Basics of R
The Very ^ 2 Basics of RThe Very ^ 2 Basics of R
The Very ^ 2 Basics of R
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
Wrokflow programming and provenance query model
Wrokflow programming and provenance query model  Wrokflow programming and provenance query model
Wrokflow programming and provenance query model
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Python data structures (Lists and tuples) presentation
Python data structures (Lists and tuples) presentationPython data structures (Lists and tuples) presentation
Python data structures (Lists and tuples) presentation
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
R statistics with mongo db
R statistics with mongo dbR statistics with mongo db
R statistics with mongo db
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 

Ähnlich wie R language, an introduction

Programming with R in Big Data Analytics
Programming with R in Big Data AnalyticsProgramming with R in Big Data Analytics
Programming with R in Big Data AnalyticsArchana Gopinath
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming FundamentalsRagia Ibrahim
 
Introduction to R
Introduction to RIntroduction to R
Introduction to RHappy Garg
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Chia-Chi Chang
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011Mandi Walls
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptxkarthikks82
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to pythonActiveState
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine LearningAmanBhalla14
 
SMS Spam Filter Design Using R: A Machine Learning Approach
SMS Spam Filter Design Using R: A Machine Learning ApproachSMS Spam Filter Design Using R: A Machine Learning Approach
SMS Spam Filter Design Using R: A Machine Learning ApproachReza Rahimi
 
India software developers conference 2013 Bangalore
India software developers conference 2013 BangaloreIndia software developers conference 2013 Bangalore
India software developers conference 2013 BangaloreSatnam Singh
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...HendraPurnama31
 

Ähnlich wie R language, an introduction (20)

R language introduction
R language introductionR language introduction
R language introduction
 
R
RR
R
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
R training3
R training3R training3
R training3
 
Programming with R in Big Data Analytics
Programming with R in Big Data AnalyticsProgramming with R in Big Data Analytics
Programming with R in Big Data Analytics
 
Big datacourse
Big datacourseBig datacourse
Big datacourse
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptx
 
Language R
Language RLanguage R
Language R
 
Ch1
Ch1Ch1
Ch1
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to python
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 
SMS Spam Filter Design Using R: A Machine Learning Approach
SMS Spam Filter Design Using R: A Machine Learning ApproachSMS Spam Filter Design Using R: A Machine Learning Approach
SMS Spam Filter Design Using R: A Machine Learning Approach
 
India software developers conference 2013 Bangalore
India software developers conference 2013 BangaloreIndia software developers conference 2013 Bangalore
India software developers conference 2013 Bangalore
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
 

Mehr von Venkatesh Prasad Ranganath

SeMA: A Design Methodology for Building Secure Android Apps
SeMA: A Design Methodology for Building Secure Android AppsSeMA: A Design Methodology for Building Secure Android Apps
SeMA: A Design Methodology for Building Secure Android AppsVenkatesh Prasad Ranganath
 
Are free Android app security analysis tools effective in detecting known vul...
Are free Android app security analysis tools effective in detecting known vul...Are free Android app security analysis tools effective in detecting known vul...
Are free Android app security analysis tools effective in detecting known vul...Venkatesh Prasad Ranganath
 
Benchpress: Analyzing Android App Vulnerability Benchmark Suites
Benchpress:  Analyzing Android App Vulnerability Benchmark SuitesBenchpress:  Analyzing Android App Vulnerability Benchmark Suites
Benchpress: Analyzing Android App Vulnerability Benchmark SuitesVenkatesh Prasad Ranganath
 
Behavior Driven Development [10] - Software Testing Techniques (CIS640)
Behavior Driven Development [10] - Software Testing Techniques (CIS640)Behavior Driven Development [10] - Software Testing Techniques (CIS640)
Behavior Driven Development [10] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Code Coverage [9] - Software Testing Techniques (CIS640)
Code Coverage [9] - Software Testing Techniques (CIS640)Code Coverage [9] - Software Testing Techniques (CIS640)
Code Coverage [9] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Boundary Value Testing [7] - Software Testing Techniques (CIS640)
Boundary Value Testing [7] - Software Testing Techniques (CIS640)Boundary Value Testing [7] - Software Testing Techniques (CIS640)
Boundary Value Testing [7] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Property Based Testing [5] - Software Testing Techniques (CIS640)
Property Based Testing [5] - Software Testing Techniques (CIS640)Property Based Testing [5] - Software Testing Techniques (CIS640)
Property Based Testing [5] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Intro to Python3 [2] - Software Testing Techniques (CIS640)
Intro to Python3 [2] - Software Testing Techniques (CIS640)Intro to Python3 [2] - Software Testing Techniques (CIS640)
Intro to Python3 [2] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Unit testing [4] - Software Testing Techniques (CIS640)
Unit testing [4] - Software Testing Techniques (CIS640)Unit testing [4] - Software Testing Techniques (CIS640)
Unit testing [4] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Testing concepts [3] - Software Testing Techniques (CIS640)
Testing concepts [3] - Software Testing Techniques (CIS640)Testing concepts [3] - Software Testing Techniques (CIS640)
Testing concepts [3] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Introduction [1] - Software Testing Techniques (CIS640)
Introduction [1] - Software Testing Techniques (CIS640)Introduction [1] - Software Testing Techniques (CIS640)
Introduction [1] - Software Testing Techniques (CIS640)Venkatesh Prasad Ranganath
 
Compatibility Testing using Patterns-based Trace Comparison
Compatibility Testing using Patterns-based Trace ComparisonCompatibility Testing using Patterns-based Trace Comparison
Compatibility Testing using Patterns-based Trace ComparisonVenkatesh Prasad Ranganath
 

Mehr von Venkatesh Prasad Ranganath (17)

SeMA: A Design Methodology for Building Secure Android Apps
SeMA: A Design Methodology for Building Secure Android AppsSeMA: A Design Methodology for Building Secure Android Apps
SeMA: A Design Methodology for Building Secure Android Apps
 
Are free Android app security analysis tools effective in detecting known vul...
Are free Android app security analysis tools effective in detecting known vul...Are free Android app security analysis tools effective in detecting known vul...
Are free Android app security analysis tools effective in detecting known vul...
 
Benchpress: Analyzing Android App Vulnerability Benchmark Suites
Benchpress:  Analyzing Android App Vulnerability Benchmark SuitesBenchpress:  Analyzing Android App Vulnerability Benchmark Suites
Benchpress: Analyzing Android App Vulnerability Benchmark Suites
 
Why do Users kill HPC Jobs?
Why do Users kill HPC Jobs?Why do Users kill HPC Jobs?
Why do Users kill HPC Jobs?
 
Behavior Driven Development [10] - Software Testing Techniques (CIS640)
Behavior Driven Development [10] - Software Testing Techniques (CIS640)Behavior Driven Development [10] - Software Testing Techniques (CIS640)
Behavior Driven Development [10] - Software Testing Techniques (CIS640)
 
Code Coverage [9] - Software Testing Techniques (CIS640)
Code Coverage [9] - Software Testing Techniques (CIS640)Code Coverage [9] - Software Testing Techniques (CIS640)
Code Coverage [9] - Software Testing Techniques (CIS640)
 
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
Equivalence Class Testing [8] - Software Testing Techniques (CIS640)
 
Boundary Value Testing [7] - Software Testing Techniques (CIS640)
Boundary Value Testing [7] - Software Testing Techniques (CIS640)Boundary Value Testing [7] - Software Testing Techniques (CIS640)
Boundary Value Testing [7] - Software Testing Techniques (CIS640)
 
Property Based Testing [5] - Software Testing Techniques (CIS640)
Property Based Testing [5] - Software Testing Techniques (CIS640)Property Based Testing [5] - Software Testing Techniques (CIS640)
Property Based Testing [5] - Software Testing Techniques (CIS640)
 
Intro to Python3 [2] - Software Testing Techniques (CIS640)
Intro to Python3 [2] - Software Testing Techniques (CIS640)Intro to Python3 [2] - Software Testing Techniques (CIS640)
Intro to Python3 [2] - Software Testing Techniques (CIS640)
 
Unit testing [4] - Software Testing Techniques (CIS640)
Unit testing [4] - Software Testing Techniques (CIS640)Unit testing [4] - Software Testing Techniques (CIS640)
Unit testing [4] - Software Testing Techniques (CIS640)
 
Testing concepts [3] - Software Testing Techniques (CIS640)
Testing concepts [3] - Software Testing Techniques (CIS640)Testing concepts [3] - Software Testing Techniques (CIS640)
Testing concepts [3] - Software Testing Techniques (CIS640)
 
Introduction [1] - Software Testing Techniques (CIS640)
Introduction [1] - Software Testing Techniques (CIS640)Introduction [1] - Software Testing Techniques (CIS640)
Introduction [1] - Software Testing Techniques (CIS640)
 
Compatibility Testing using Patterns-based Trace Comparison
Compatibility Testing using Patterns-based Trace ComparisonCompatibility Testing using Patterns-based Trace Comparison
Compatibility Testing using Patterns-based Trace Comparison
 
My flings with data analysis
My flings with data analysisMy flings with data analysis
My flings with data analysis
 
Data analytics, a (short) tour
Data analytics, a (short) tourData analytics, a (short) tour
Data analytics, a (short) tour
 
Pattern-based Features
Pattern-based FeaturesPattern-based Features
Pattern-based Features
 

Kürzlich hochgeladen

How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Kürzlich hochgeladen (20)

Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 

R language, an introduction

  • 1. The R Language A Hands-on Introduction Venkatesh-Prasad Ranganath http://about.me/rvprasad
  • 2. What is R? • A dynamical typed programming language • http://cran.r-project.org/ • Open source and free • Provides common programming language constructs/features • Multiple programming paradigms • Numerous libraries focused on various data-rich topics • http://cran.r-project.org/web/views/ • Ideal for statistical calculation; lately, the go-to tool for data analysis • Accompanied by RStudio, a simple and powerful IDE • http://rstudio.org
  • 3. Data Types (Modes) • Numeric • Character • Logical (TRUE / FALSE) • Complex • Raw (bytes)
  • 4. Data Structures • Vectors • Matrices • Arrays • Lists • Data Frames • Factors • Tables
  • 5. Data Structures: Vectors • A sequence of objects of the same (atomic) data type • Creation • x <- b c [ <- is the assignment operator ] • y <- seq(5, 9, 2) = c(5, 7, 9) • y <- 5:7 = c(5, 6, 7) [ m:n is equivalent to seq(m, n, 1) ] • y <- c(1, 4:6) = c(1, 4, 5, 6) [ no nesting / always flattened ] • z <- rep(1, 3) = c(1, 1, 1)
  • 6. Data Structures: Vectors • Accessing • x[1] [ 1-based indexing ] • x[2:3] • x[c(2,3)] = x[2:3] • x[-1] [ Negative subscripts imply exclusion ] • Naming • names(x) <- [ Makes equivalent to x[1] ]
  • 7. Data Structures: Vectors • Operations • x <- c(5, 6, 7) • x + 2 = c(7, 8, 9) [ Vectorized operations ] • x > 5 = c(FALSE, TRUE, TRUE) • subset(x, x > 5) = c(6, 7) • which(x > 5) = c(2, 3) • ifelse(x > 5, NaN, x) = c(5, NaN, NaN) • sqr <- function (n) { n * n } • sapply(x,sqr) = c(25 ,36, 49) • sqr(x) = c(25, 36, 49)
  • 8. Data Structures: Vectors • Operations • x <- c(5, 6, 7) • any(x > 5) = TRUE [ How about all(x > 5)? ] • sum(c(1, 2, 3, NA), na.rm = TRUE) = 6 [ Why is na.rm required? ] • sort(c(7, 6, 5)) = c(5, 6, 7) • order(c(7, 6, 5)) = ??? • subset(x, x > 5) = c(6, 7) • head(1:100) = ??? • tail(1:100) = ??? • How is x == c(5, 6, 7) different from identical(x, c(5, 6, 7))? • Try str(x)
  • 9. Data Structures: Matrices • A two dimensional matrix of objects of the same (atomic) data type • Creation • y <- matrix(nrow=2, ncol=3) [ empty matrix ] • y <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2) = • y <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2, byrow=T) = • Accessing • y[1,2] = 2 • y[,2:3] = [ How about y[1,]? ] • What’s the difference between y[2,] and y[2,, drop=FALSE]? 1 3 5 2 4 6 1 2 3 4 5 6 2 3 5 6
  • 10. Data Structures: Matrices • Naming • rownames() and colnames() • Operations • nrow(y) = 2 [ number of rows ] • ncol(y) = 3 [ number of columns ] • apply(y, 1, sum) = c(6, 15) [ apply sum to each row ] • apply(y, 2, sum) = c(5, 7, 9) [ apply sum to each column ] • t(y) = [ transpose a matrix ]1 4 2 5 3 6
  • 11. Data Structures: Matrices • Operations • rbind(y, c(7, 8, 9)) = • cbind(y, c(7, 8)) = • Try str(y) 1 2 3 4 5 6 7 8 9 1 2 3 7 4 5 6 8
  • 12. Data Structures: Matrices • What will this yield? m <- matrix(nrow=4, ncol=4) m <- ifelse(row(m) == col(m), 1, 0.3)
  • 13. Data Structures: Lists • A sequence of objects (of possibly different data types) • Creation • k <- list(c(1, 2, 3), • l <- [ f1 and f2 are tags ] • Accessing • k[2:3] • k[[2]] [ How about k[2]? ] • l$f1 = c(1, 2, 3) [ Is it same as l[1] or l[[1]]? ]
  • 14. Data Structures: Lists • Naming • names(k) <- • Operations • lapply(list(1:2, 9:10), sum) = list(3, 19) • sapply(list(1:2, 9:10), sum) = c(3, 19) • l$f1 <- NULL = ??? • str(l) = ???
  • 15. Data Structures: Data Frames • A two dimensional matrix of objects where different columns can be of different types. • Creation • x <- data.frame jill • Accessing • x$names jill [ How about x[[1]]? ] • x[1] = ??? • x[c(1,2)] = ??? • x[1,] = ??? • x[,1] = ???
  • 16. Data Structures: Data Frames • Naming • rownames() and colnames() • Operations • x[x$age > 5,] = data.frame jill )) • subset(x, age > 5) = ??? • apply(x, 1, sum) = ??? • y <- data.frame(1:3, 5:7) • apply(y, 1, mean) = ??? • lapply(y, mean) = ??? • sapply(y, mean) = ??? • Try str(y)
  • 17. Factors (and Tables) • Type for categorical/nominal values. • Example • xf <- factor(c(1:3, 2, 4:5)) • Try xf and str(xf) • Operations • table(xf) = ??? • with(mtcars, split(mpg, cyl)) = ??? • with(mtcars, tapply(mpg, cyl, mean)) = ??? • by(mtcars, mtcars$cyl, function(m) { median(m$mpg) } = ??? • aggregate(mtcars, list(mtcars$cyl), median) = ??? • You can use cut to bin values and create factors. Try it.
  • 18. Basic Graphs • with(mtcars, boxplot(mpg)) • hist(mtcars$mpg) • with(mtcars, plot(hp, mpg)) • dotchart(VADeaths) • Try plot(aggregate(mtcars, list(mtcars$cyl), median)) You can get the list of datasets via ls package.datasets
  • 19. Stats 101 using R • mean • median • What about mode? • fivenum • quantile • sd • var • cov • cor
  • 20. Data Exploration using R Let’s get out hands dirty!!