SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Downloaden Sie, um offline zu lesen
R Language

dwivedishashwat@gmail.com
Background

• 1991: Created by Ross Ihaka and Robert Gentleman
• 2000: R version 1.0.0 is released
• Latest version is 2.15.2 released in Oct „12

• R version 2.15.3 is scheduled to release in Mar „12
and 3.0.0 is scheduled to be released in Apr ‟13
• http://www.r-project.org (basic information about R)
• http://www.cran.r-project.org (base system and
additional packages)
• help() or ?help, help.search() or ??help
Background

• R is a free software environment for
statistical computing and graphics
• Very active and vibrant user community
• Graphical capabilities
• Physical memory
• Base R and around 4000 packages

1/21/2014
Introduction
 memory.limit(): To find out maximum amount of available
physical memory
 memory.size(): To find out how much memory is in use
 getwd(): Shows the path of your current working directory
 setwd(path): Allows you to set a new path for your current
working directory

 dir(): List down all the files in your working directory
 Program Editor (open, load, run, save)
• ls(): List all objects in your workspace

• rm(): Removes object from your workspace
Introduction

 Commands to R are expressions (4/3) or assignments (x <- 4/3)
 R is case sensitive
 Everything in R is a object
 Normally R objects are accessed by their names which is made up from letters,
and digits (0 to 9) or a period (“.”) in non-initial positions.
 Every object has a class
 R has 5 basic classes of objects
 character
 numeric (real numbers)
 integers
 complex
 logical (True / False)

•

The most basic object is a vector

 A vector can only contains objects of the same class
Background
•

Ex.

•

 x <- 1 # assignment
 Print(x) # explicit printing
 X # auto printing
Ex.









•

Q.




•

x <- c(0.5, 0.6) # numeric
X <- c(TRUE, FALSE) # logical
X <- c(T, F) # logical
X <- c(“a”, “b”, “c”, “d”) # character
X <- 1:20 # integer
X <- c(1+0i, 2+4i) #complex
seq(from=1, to=10, by=1)
rep(c(1,2,3,4,5), times=2, each=2)

X <- c(1.7, “a”)
X <- c(TRUE, 2)
X <- c(“a”, TRUE)

When different objects are mixed in a vector, coercion occurs so that every element in the
vector is of same class
Session 1 Remaining

• rep(x, times, length.out, each)
• rm()
• rm(list=ls(pattern=“^test”))
• rm(list=ls(pattern=“test”))
• rm(list=setdiff(ls(), “test”))
• rm(list=ls())
Introduction

• Ex.
 X <- 0:6
 X <- c(“a”, “b”, “c”)
 X <- c(1, 2, 3)

 Numbers in R generally treated as numeric (i.e. double precision
real numbers)
 If you explicitly wants an integer then you need to specify L suffix
 Special number Inf (1/0), it actually a real number, 1/Inf will give
you 0
 Undefined value NaN (0/0) Not a Number, it can be though of as
missing value
• # indicates comments
Data Types

 R objects can have attributes (attributes())
 Class (class())
 Length (length())
 names (colnames for a matrix), dimnames (rownames, colnames for a matrix)

 dimensions (dim())
 other user defined attributes

 Various data types in R
 Vectors
 Vector(mode, length)

•

Lists: Special type of vectors which can contain objects of different
classes.

 x <- list(1,2,3,“a”,”b”,”c”)
 x <- list(a=c(1,2,3), b=1:4, c=c(“a”,”b”,”c”))
Data Types
 Matrix: vectors with dimension attribute. Dimension itself is an
integer vector of length 2 (nrow, ncol). Matrices are constructed
column wise.







m <- matrix(nrow=2, ncol=3)
m <- matrix(1:6, nrow=2, ncol=3)
x <- 1:3
y <- 10:12
cbind(x, y)
rbind(x,y)

 Data frames (data.frame())
 https://stat.ethz.ch/pipermail/rhelp/attachments/20101027/05a229bb/attachment.pl
 Factors: Used for categorical data i.e. Male & Female or analyst,
senior analyst, manager etc.






x <- factor(c(“a”, “b”, “b”, “c”, “c”, “c”, “d”))
levels()
unclass(x)
levels([4:6])
Levels([4:6, drop=TRUE])
Date & Time

 Converting a character variable to a date variable
 as.Date(variable_name, input_format)
 strptime(variable_name, input_format)
 Output will be %Y-%m-%d %H:%M:%S


%Y: Year with century



%m: Month as decimal number (01-12)



%d: Day of the month as decimal number(01-31)



%H: Hrs as decimal numbers (00-23)



%M: Minutes as decimal numbers (00-59)



%S: Seconda as decimal numbers (00-59)

 Converting a date variable to a character variable / formatting a date
variable
 strftime(date_variable_name, output_format)
 format(data_variable_name, output_format)
 as.character(date_variable_name, output_format)
Sub-setting

 [ always returns an object of the same class as the original; can be
used to select more than one element
 [[ is used to extract elements of list or data frames; it can only be
used to extract single element and the class of the returned object
will not necessarily be a list or data frame
 $ is used to extract elements of a list or data frames by names;
semantics are similar to [[
Operators

 <: Less than
 <=: Less than equals to
 >: Greater than
 >=: Greater than equals to
 ==: Exactly equals to
 !=: Not equal to

 | or II: OR
 & or &&: AND
 !: NOT
Some Examples

 x <- c(“a”, “b”, “c”, “c”, “d”, “a”)
 x[1], x[1:4], x[x > “a”], u <- x >”a”

 x <- matrix(1:6,2,3)
 x[1,2], x[1,], x[,1], x[1,2, drop=FALSE]

 x <- list(var_1=c(1:10), var_2=c(“a”, “b”, “c”), var_3=0.6)
 x[1], x[[1]], x$var_1
 name <- “var_1”, x[name], x[[name]], x$name
 x[c(1,3)], x[[c(1,3)]], x[[1]][[3]]

 Produce a character vector containing var_1, var_2, var_3… var_999
 Remove missing values from x <- c(1, 2, 3, NA, 4, 5, NA, 6)
 y <- c(“a”, “b”, NA, NA, “c”, “d”, “e”, “f”), prepare a matrix containing
two columns x & y and does not have any missing value
 What is the sum & mean of Wind for the observations which has
temperature greater then 60 & month equals to 5
 How to create a new directory with a given name
Reading / Writing Data Set
 Principle functions for reading data into R
 read.table(), read.csv(): Used for reading tabular data
 readLines(): For reading lines of a text file
 source(): For reading in R code file
 dget(): For reading in R code file
 load(): For reading in saved workspaces
 unserialize(): For reading single R objects in binary form

•

Principle functions for writing data to files

 write.table()
 writeLines()
 dump()
 dput()
 save()
 serialize()
Importing / Exporting Data

 Read.table() is one of the most commonly used function for reading data.
Few important arguments;
 file, name of the file to be read,
 header, logical indicating if the file has a header line
 sep, a string indicating how the columns are separated
 colClasses, a character vector indicating class of each column in the dataset
 nrows, the maximum number of rows to be read in the dataset
 na.strings, a character vector of strings which are to be interpreted as NA values
 comment.char, a character string indicating the comment character
 skip, number of lines to skip from beginning
 stringAsFactors, logical indicating should character variables be codes as factors

 Write.table()
 X, the object to be written, preferable a matrix or a data frame
 File, path and name of the file to be created
 Sep, a string indicating how the columns are separated
 Row.names, col.names, logical indicating whether the row names or col names to be
written along with x
Data Summary / Manipulation

 attach(x): For attaching a file
 detach(x): For detaching a file
•

summary(x): For displaying summary statistics of a data set

•

str(x): For displaying summary statistics of a data set in a different
manner then summary()

•

sort(): For sorting a vector or factor

•

order(): For ordering along more than one variable

•

merge(): Merge two data frames by common columns or row names, or
do other versions of database join operations

•

cut(x, breaks, labels): Divides the range of x into intervals and codes
the values in x according to which interval they fall. The leftmost
interval corresponds to level one, the next leftmost to level two and
so on.
 cut(x, 10, 1:10)
Data Summary / Manipulation

•

pretty(x, n): Compute a sequence of about n+1 equally spaced „round‟
values which cover the range of the values in x.
 pretty(x, 100)

•

substr(x, start, stop) <- value: Extract or replace substrings in a
character vector.

•

strsplit(): Split the elements of a character vector x into substrings
according to the matches to substring split within them.

•

rank(): Returns the sample ranks of the values in a vector. Ties (i.e.,
equal values) and missing values can be handled in several ways

•

aggregate(): Splits the data into subsets, computes summary statistics
for each, and returns the result in a convenient form.

 ddply(): For each subset of a data frame, apply function then combine
results into a data frame.
Control Structures

 Allows you to control the flow of execution of the program
 if, else (testing a condition)
 if (condition) {do something} else if {do something different} else {do something
different}

 for (executing a loop fixed number of times)
 for (i in 1:10) { do something}

 while (executing a loop while a condition is true)
 while (condition) { do something}

 repeat (execute a infinite loop)

 break (break the execution of a loop)
 next (skip a iteration of a loop)
 return (exit a function)

 Create a vector with all integers from 1 to 1000 and replace all even
number by their inverse
Loop Functions

 lapply: Returns a list of the same length as X, each element of which
is the result of applying FUN to the corresponding element of X
 lapply(airquality, mean)
 Calculate sum of all the variables of the airquality dataset excluding NAs

 sapply: Sapply is a user-friendly version of lapply by default returning
a vector or matrix if appropriate
 sapply(airquality, mean)
 Repeat the problem present in lapply using sapply and see the difference

 apply: Returns a vector or array or list of values obtained by applying
a function to margins of an array or matrix
 apply(airquality, 1, sum)
 Calculate deciles including min and max of all the variables of the dataset
airquality excluding NAs
 Calculate square of each element of a matrix with dimensions 10 & 2 and
entries 1 to 20
Loop Functions

 tapply: Apply a function to each cell of a ragged array, that is to
each (non-empty) group of values given by a unique combination of
the levels of certain factors
 tapply(airquality$Ozone, aiqruality$Month, sum)

 Calculate sum of Ozone variable for observations having month equals
to 5

 mapply: mapply is a multivariate version of sapply. mapply applies
FUN to the first elements of each argument, the second elements,
the third elements, and so on
 mapply(rep, 1:4, 4:1)
 Calculate sum of two lists with dimensions 10 & 2 and having entries 1
to 20, 101 to 120, 201 to 220 & 301 to 320
Plotting Functions
 plot(x,y)
 hist(x)
 par()
 pch: plotting symbol

 lty: line type
 lwd: line width
 col: plotting color
 las: axis label orientation
 bg: background color

 mar: margin size
 oma: outer margin size
 mfrow: number of plots per row, column (plots are filled row-wise)
 mfcol: number of plots per row, column (plots are filled column-wise)
Plotting Functions

 lines: add lines to the plot
 points: add points to the plot
 text: add text labels to the plot
 title: add annotations to x, y axis labels, title, subtitle, outer
margin

 mtext: add text to the margins of the plot
 axis: adding axis ticks/labels
Functions

 function ()
 Exact match –> Partial match –> Positional match
 Return value of a function is the last expression in the function body
to be evaluated
 Functions can be nested, so that a function can be defined inside
another function
 Functions can be passed as arguments to other functions
Debugging

• Primary tools for debugging functions in R
 traceback: prints out the function call stack after an error occurs; does
nothing if there is no error
 debug: flags a function for debug mode which allows you to step through
execution of a function one line at a time
 browser: suspends the execution of a function whenever it is called and
puts the function in debug mode
 trace: allows you to insert debugging code into a function at specific
places
 recover: allows you to modify the error behavior so that you can browse
the function call stack
Debugging

 Indications that something‟s is not right
 message: a generic notification/diagnostic message produced by the
message function; execution of the function continues

 warning: an indication that something is wrong but not necessarily
fatal produced by warning function‟ execution of the function
continues
 error: an indication that a fatal problem has occurred produced by
stop function; execution stops
 condition: a generic concept for indicating that something
unexpected can occur; programmers can create their own conditions
Thanks a lot
For Question Read more 

dwivedishashwat@gmail.com

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction To R Language
Introduction To R LanguageIntroduction To R Language
Introduction To R LanguageGaurang Dobariya
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in RFlorian Uhlitz
 
R programming intro with examples
R programming intro with examplesR programming intro with examples
R programming intro with examplesDennis
 
R basics
R basicsR basics
R basicsFAO
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine LearningAmanBhalla14
 
3 R Tutorial Data Structure
3 R Tutorial Data Structure3 R Tutorial Data Structure
3 R Tutorial Data StructureSakthi Dasans
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching moduleSander Timmer
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query LanguageJulian Hyde
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in RJeffrey Breen
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingAlberto Labarga
 
2 R Tutorial Programming
2 R Tutorial Programming2 R Tutorial Programming
2 R Tutorial ProgrammingSakthi Dasans
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiUnmesh Baile
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data framekrishna singh
 

Was ist angesagt? (20)

Introduction To R Language
Introduction To R LanguageIntroduction To R Language
Introduction To R Language
 
Language R
Language RLanguage R
Language R
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in R
 
R programming intro with examples
R programming intro with examplesR programming intro with examples
R programming intro with examples
 
R language
R languageR language
R language
 
R basics
R basicsR basics
R basics
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 
3 R Tutorial Data Structure
3 R Tutorial Data Structure3 R Tutorial Data Structure
3 R Tutorial Data Structure
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching module
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query Language
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
 
Data Management in Python
Data Management in PythonData Management in Python
Data Management in Python
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
2 R Tutorial Programming
2 R Tutorial Programming2 R Tutorial Programming
2 R Tutorial Programming
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbai
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
R factors
R   factorsR   factors
R factors
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
 

Andere mochten auch

An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)Dataspora
 
Classification Model - Decision Tree
Classification Model -  Decision TreeClassification Model -  Decision Tree
Classification Model - Decision TreeVaibhav Jain
 
Classification model for predicting student's knowledge
Classification model for predicting student's knowledgeClassification model for predicting student's knowledge
Classification model for predicting student's knowledgeAshish Ranjan
 
IDS alert classification model
IDS alert classification modelIDS alert classification model
IDS alert classification modeldilipjangam91
 
Simple Business Model Classification System: Business Model Pipes, Valleys, a...
Simple Business Model Classification System: Business Model Pipes, Valleys, a...Simple Business Model Classification System: Business Model Pipes, Valleys, a...
Simple Business Model Classification System: Business Model Pipes, Valleys, a...Rod King, Ph.D.
 
Introduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing EnvironmentIntroduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing Environmentizahn
 
Data Analytics with Pandas and Numpy - Python
Data Analytics with Pandas and Numpy - PythonData Analytics with Pandas and Numpy - Python
Data Analytics with Pandas and Numpy - PythonChetan Khatri
 
R programming Basic & Advanced
R programming Basic & AdvancedR programming Basic & Advanced
R programming Basic & AdvancedSohom Ghosh
 
Major issues in data mining
Major issues in data miningMajor issues in data mining
Major issues in data miningSlideshare
 
Chapter 4 Classification
Chapter 4 ClassificationChapter 4 Classification
Chapter 4 ClassificationKhalid Elshafie
 
R language tutorial
R language tutorialR language tutorial
R language tutorialDavid Chiu
 
Data Mining: Classification and analysis
Data Mining: Classification and analysisData Mining: Classification and analysis
Data Mining: Classification and analysisDataminingTools Inc
 
Data mining: Classification and prediction
Data mining: Classification and predictionData mining: Classification and prediction
Data mining: Classification and predictionDataminingTools Inc
 
R Language definition
R Language definitionR Language definition
R Language definition湘云 黄
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedSlideShare
 

Andere mochten auch (17)

An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
 
Classification Model - Decision Tree
Classification Model -  Decision TreeClassification Model -  Decision Tree
Classification Model - Decision Tree
 
Classification model for predicting student's knowledge
Classification model for predicting student's knowledgeClassification model for predicting student's knowledge
Classification model for predicting student's knowledge
 
IDS alert classification model
IDS alert classification modelIDS alert classification model
IDS alert classification model
 
Simple Business Model Classification System: Business Model Pipes, Valleys, a...
Simple Business Model Classification System: Business Model Pipes, Valleys, a...Simple Business Model Classification System: Business Model Pipes, Valleys, a...
Simple Business Model Classification System: Business Model Pipes, Valleys, a...
 
Introduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing EnvironmentIntroduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing Environment
 
LSESU a Taste of R Language Workshop
LSESU a Taste of R Language WorkshopLSESU a Taste of R Language Workshop
LSESU a Taste of R Language Workshop
 
Data Analytics with Pandas and Numpy - Python
Data Analytics with Pandas and Numpy - PythonData Analytics with Pandas and Numpy - Python
Data Analytics with Pandas and Numpy - Python
 
R programming Basic & Advanced
R programming Basic & AdvancedR programming Basic & Advanced
R programming Basic & Advanced
 
Major issues in data mining
Major issues in data miningMajor issues in data mining
Major issues in data mining
 
Chapter 4 Classification
Chapter 4 ClassificationChapter 4 Classification
Chapter 4 Classification
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
Data Mining: Classification and analysis
Data Mining: Classification and analysisData Mining: Classification and analysis
Data Mining: Classification and analysis
 
Data mining: Classification and prediction
Data mining: Classification and predictionData mining: Classification and prediction
Data mining: Classification and prediction
 
R Language definition
R Language definitionR Language definition
R Language definition
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-Presented
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Ähnlich wie R language introduction

Ähnlich wie R language introduction (20)

R Programming Reference Card
R Programming Reference CardR Programming Reference Card
R Programming Reference Card
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
R Introduction
R IntroductionR Introduction
R Introduction
 
R교육1
R교육1R교육1
R교육1
 
R workshop
R workshopR workshop
R workshop
 
statistical computation using R- an intro..
statistical computation using R- an intro..statistical computation using R- an intro..
statistical computation using R- an intro..
 
3 Data Structure in R
3 Data Structure in R3 Data Structure in R
3 Data Structure in R
 
20170509 rand db_lesugent
20170509 rand db_lesugent20170509 rand db_lesugent
20170509 rand db_lesugent
 
Practical data science_public
Practical data science_publicPractical data science_public
Practical data science_public
 
Reference card for R
Reference card for RReference card for R
Reference card for R
 
Short Reference Card for R users.
Short Reference Card for R users.Short Reference Card for R users.
Short Reference Card for R users.
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 
R Basics
R BasicsR Basics
R Basics
 
@ R reference
@ R reference@ R reference
@ R reference
 
R command cheatsheet.pdf
R command cheatsheet.pdfR command cheatsheet.pdf
R command cheatsheet.pdf
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 

Mehr von Shashwat Shriparv (20)

Learning Linux Series Administrator Commands.pptx
Learning Linux Series Administrator Commands.pptxLearning Linux Series Administrator Commands.pptx
Learning Linux Series Administrator Commands.pptx
 
Kerberos Architecture.pptx
Kerberos Architecture.pptxKerberos Architecture.pptx
Kerberos Architecture.pptx
 
Kerberos Architecture.pptx
Kerberos Architecture.pptxKerberos Architecture.pptx
Kerberos Architecture.pptx
 
Command Seperators.pptx
Command Seperators.pptxCommand Seperators.pptx
Command Seperators.pptx
 
Upgrading hadoop
Upgrading hadoopUpgrading hadoop
Upgrading hadoop
 
Hadoop migration and upgradation
Hadoop migration and upgradationHadoop migration and upgradation
Hadoop migration and upgradation
 
Hive query optimization infinity
Hive query optimization infinityHive query optimization infinity
Hive query optimization infinity
 
H base introduction & development
H base introduction & developmentH base introduction & development
H base introduction & development
 
Hbase interact with shell
Hbase interact with shellHbase interact with shell
Hbase interact with shell
 
H base development
H base developmentH base development
H base development
 
Hbase
HbaseHbase
Hbase
 
H base
H baseH base
H base
 
My sql
My sqlMy sql
My sql
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
 
Linux 4 you
Linux 4 youLinux 4 you
Linux 4 you
 
Introduction to apache hadoop
Introduction to apache hadoopIntroduction to apache hadoop
Introduction to apache hadoop
 
Next generation technology
Next generation technologyNext generation technology
Next generation technology
 
Configure h base hadoop and hbase client
Configure h base hadoop and hbase clientConfigure h base hadoop and hbase client
Configure h base hadoop and hbase client
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 

Kürzlich hochgeladen

How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17Celine George
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfNOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfSumit Tiwari
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
How to Filter Blank Lines in Odoo 17 Accounting
How to Filter Blank Lines in Odoo 17 AccountingHow to Filter Blank Lines in Odoo 17 Accounting
How to Filter Blank Lines in Odoo 17 AccountingCeline George
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICESayali Powar
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.raviapr7
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 

Kürzlich hochgeladen (20)

How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfNOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
How to Filter Blank Lines in Odoo 17 Accounting
How to Filter Blank Lines in Odoo 17 AccountingHow to Filter Blank Lines in Odoo 17 Accounting
How to Filter Blank Lines in Odoo 17 Accounting
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICE
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 

R language introduction

  • 2. Background • 1991: Created by Ross Ihaka and Robert Gentleman • 2000: R version 1.0.0 is released • Latest version is 2.15.2 released in Oct „12 • R version 2.15.3 is scheduled to release in Mar „12 and 3.0.0 is scheduled to be released in Apr ‟13 • http://www.r-project.org (basic information about R) • http://www.cran.r-project.org (base system and additional packages) • help() or ?help, help.search() or ??help
  • 3. Background • R is a free software environment for statistical computing and graphics • Very active and vibrant user community • Graphical capabilities • Physical memory • Base R and around 4000 packages 1/21/2014
  • 4. Introduction  memory.limit(): To find out maximum amount of available physical memory  memory.size(): To find out how much memory is in use  getwd(): Shows the path of your current working directory  setwd(path): Allows you to set a new path for your current working directory  dir(): List down all the files in your working directory  Program Editor (open, load, run, save) • ls(): List all objects in your workspace • rm(): Removes object from your workspace
  • 5. Introduction  Commands to R are expressions (4/3) or assignments (x <- 4/3)  R is case sensitive  Everything in R is a object  Normally R objects are accessed by their names which is made up from letters, and digits (0 to 9) or a period (“.”) in non-initial positions.  Every object has a class  R has 5 basic classes of objects  character  numeric (real numbers)  integers  complex  logical (True / False) • The most basic object is a vector  A vector can only contains objects of the same class
  • 6. Background • Ex. •  x <- 1 # assignment  Print(x) # explicit printing  X # auto printing Ex.         • Q.    • x <- c(0.5, 0.6) # numeric X <- c(TRUE, FALSE) # logical X <- c(T, F) # logical X <- c(“a”, “b”, “c”, “d”) # character X <- 1:20 # integer X <- c(1+0i, 2+4i) #complex seq(from=1, to=10, by=1) rep(c(1,2,3,4,5), times=2, each=2) X <- c(1.7, “a”) X <- c(TRUE, 2) X <- c(“a”, TRUE) When different objects are mixed in a vector, coercion occurs so that every element in the vector is of same class
  • 7. Session 1 Remaining • rep(x, times, length.out, each) • rm() • rm(list=ls(pattern=“^test”)) • rm(list=ls(pattern=“test”)) • rm(list=setdiff(ls(), “test”)) • rm(list=ls())
  • 8. Introduction • Ex.  X <- 0:6  X <- c(“a”, “b”, “c”)  X <- c(1, 2, 3)  Numbers in R generally treated as numeric (i.e. double precision real numbers)  If you explicitly wants an integer then you need to specify L suffix  Special number Inf (1/0), it actually a real number, 1/Inf will give you 0  Undefined value NaN (0/0) Not a Number, it can be though of as missing value • # indicates comments
  • 9. Data Types  R objects can have attributes (attributes())  Class (class())  Length (length())  names (colnames for a matrix), dimnames (rownames, colnames for a matrix)  dimensions (dim())  other user defined attributes  Various data types in R  Vectors  Vector(mode, length) • Lists: Special type of vectors which can contain objects of different classes.  x <- list(1,2,3,“a”,”b”,”c”)  x <- list(a=c(1,2,3), b=1:4, c=c(“a”,”b”,”c”))
  • 10. Data Types  Matrix: vectors with dimension attribute. Dimension itself is an integer vector of length 2 (nrow, ncol). Matrices are constructed column wise.       m <- matrix(nrow=2, ncol=3) m <- matrix(1:6, nrow=2, ncol=3) x <- 1:3 y <- 10:12 cbind(x, y) rbind(x,y)  Data frames (data.frame())  https://stat.ethz.ch/pipermail/rhelp/attachments/20101027/05a229bb/attachment.pl  Factors: Used for categorical data i.e. Male & Female or analyst, senior analyst, manager etc.      x <- factor(c(“a”, “b”, “b”, “c”, “c”, “c”, “d”)) levels() unclass(x) levels([4:6]) Levels([4:6, drop=TRUE])
  • 11. Date & Time  Converting a character variable to a date variable  as.Date(variable_name, input_format)  strptime(variable_name, input_format)  Output will be %Y-%m-%d %H:%M:%S  %Y: Year with century  %m: Month as decimal number (01-12)  %d: Day of the month as decimal number(01-31)  %H: Hrs as decimal numbers (00-23)  %M: Minutes as decimal numbers (00-59)  %S: Seconda as decimal numbers (00-59)  Converting a date variable to a character variable / formatting a date variable  strftime(date_variable_name, output_format)  format(data_variable_name, output_format)  as.character(date_variable_name, output_format)
  • 12. Sub-setting  [ always returns an object of the same class as the original; can be used to select more than one element  [[ is used to extract elements of list or data frames; it can only be used to extract single element and the class of the returned object will not necessarily be a list or data frame  $ is used to extract elements of a list or data frames by names; semantics are similar to [[
  • 13. Operators  <: Less than  <=: Less than equals to  >: Greater than  >=: Greater than equals to  ==: Exactly equals to  !=: Not equal to  | or II: OR  & or &&: AND  !: NOT
  • 14. Some Examples  x <- c(“a”, “b”, “c”, “c”, “d”, “a”)  x[1], x[1:4], x[x > “a”], u <- x >”a”  x <- matrix(1:6,2,3)  x[1,2], x[1,], x[,1], x[1,2, drop=FALSE]  x <- list(var_1=c(1:10), var_2=c(“a”, “b”, “c”), var_3=0.6)  x[1], x[[1]], x$var_1  name <- “var_1”, x[name], x[[name]], x$name  x[c(1,3)], x[[c(1,3)]], x[[1]][[3]]  Produce a character vector containing var_1, var_2, var_3… var_999  Remove missing values from x <- c(1, 2, 3, NA, 4, 5, NA, 6)  y <- c(“a”, “b”, NA, NA, “c”, “d”, “e”, “f”), prepare a matrix containing two columns x & y and does not have any missing value  What is the sum & mean of Wind for the observations which has temperature greater then 60 & month equals to 5  How to create a new directory with a given name
  • 15. Reading / Writing Data Set  Principle functions for reading data into R  read.table(), read.csv(): Used for reading tabular data  readLines(): For reading lines of a text file  source(): For reading in R code file  dget(): For reading in R code file  load(): For reading in saved workspaces  unserialize(): For reading single R objects in binary form • Principle functions for writing data to files  write.table()  writeLines()  dump()  dput()  save()  serialize()
  • 16. Importing / Exporting Data  Read.table() is one of the most commonly used function for reading data. Few important arguments;  file, name of the file to be read,  header, logical indicating if the file has a header line  sep, a string indicating how the columns are separated  colClasses, a character vector indicating class of each column in the dataset  nrows, the maximum number of rows to be read in the dataset  na.strings, a character vector of strings which are to be interpreted as NA values  comment.char, a character string indicating the comment character  skip, number of lines to skip from beginning  stringAsFactors, logical indicating should character variables be codes as factors  Write.table()  X, the object to be written, preferable a matrix or a data frame  File, path and name of the file to be created  Sep, a string indicating how the columns are separated  Row.names, col.names, logical indicating whether the row names or col names to be written along with x
  • 17. Data Summary / Manipulation  attach(x): For attaching a file  detach(x): For detaching a file • summary(x): For displaying summary statistics of a data set • str(x): For displaying summary statistics of a data set in a different manner then summary() • sort(): For sorting a vector or factor • order(): For ordering along more than one variable • merge(): Merge two data frames by common columns or row names, or do other versions of database join operations • cut(x, breaks, labels): Divides the range of x into intervals and codes the values in x according to which interval they fall. The leftmost interval corresponds to level one, the next leftmost to level two and so on.  cut(x, 10, 1:10)
  • 18. Data Summary / Manipulation • pretty(x, n): Compute a sequence of about n+1 equally spaced „round‟ values which cover the range of the values in x.  pretty(x, 100) • substr(x, start, stop) <- value: Extract or replace substrings in a character vector. • strsplit(): Split the elements of a character vector x into substrings according to the matches to substring split within them. • rank(): Returns the sample ranks of the values in a vector. Ties (i.e., equal values) and missing values can be handled in several ways • aggregate(): Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form.  ddply(): For each subset of a data frame, apply function then combine results into a data frame.
  • 19. Control Structures  Allows you to control the flow of execution of the program  if, else (testing a condition)  if (condition) {do something} else if {do something different} else {do something different}  for (executing a loop fixed number of times)  for (i in 1:10) { do something}  while (executing a loop while a condition is true)  while (condition) { do something}  repeat (execute a infinite loop)  break (break the execution of a loop)  next (skip a iteration of a loop)  return (exit a function)  Create a vector with all integers from 1 to 1000 and replace all even number by their inverse
  • 20. Loop Functions  lapply: Returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X  lapply(airquality, mean)  Calculate sum of all the variables of the airquality dataset excluding NAs  sapply: Sapply is a user-friendly version of lapply by default returning a vector or matrix if appropriate  sapply(airquality, mean)  Repeat the problem present in lapply using sapply and see the difference  apply: Returns a vector or array or list of values obtained by applying a function to margins of an array or matrix  apply(airquality, 1, sum)  Calculate deciles including min and max of all the variables of the dataset airquality excluding NAs  Calculate square of each element of a matrix with dimensions 10 & 2 and entries 1 to 20
  • 21. Loop Functions  tapply: Apply a function to each cell of a ragged array, that is to each (non-empty) group of values given by a unique combination of the levels of certain factors  tapply(airquality$Ozone, aiqruality$Month, sum)  Calculate sum of Ozone variable for observations having month equals to 5  mapply: mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each argument, the second elements, the third elements, and so on  mapply(rep, 1:4, 4:1)  Calculate sum of two lists with dimensions 10 & 2 and having entries 1 to 20, 101 to 120, 201 to 220 & 301 to 320
  • 22. Plotting Functions  plot(x,y)  hist(x)  par()  pch: plotting symbol  lty: line type  lwd: line width  col: plotting color  las: axis label orientation  bg: background color  mar: margin size  oma: outer margin size  mfrow: number of plots per row, column (plots are filled row-wise)  mfcol: number of plots per row, column (plots are filled column-wise)
  • 23. Plotting Functions  lines: add lines to the plot  points: add points to the plot  text: add text labels to the plot  title: add annotations to x, y axis labels, title, subtitle, outer margin  mtext: add text to the margins of the plot  axis: adding axis ticks/labels
  • 24. Functions  function ()  Exact match –> Partial match –> Positional match  Return value of a function is the last expression in the function body to be evaluated  Functions can be nested, so that a function can be defined inside another function  Functions can be passed as arguments to other functions
  • 25. Debugging • Primary tools for debugging functions in R  traceback: prints out the function call stack after an error occurs; does nothing if there is no error  debug: flags a function for debug mode which allows you to step through execution of a function one line at a time  browser: suspends the execution of a function whenever it is called and puts the function in debug mode  trace: allows you to insert debugging code into a function at specific places  recover: allows you to modify the error behavior so that you can browse the function call stack
  • 26. Debugging  Indications that something‟s is not right  message: a generic notification/diagnostic message produced by the message function; execution of the function continues  warning: an indication that something is wrong but not necessarily fatal produced by warning function‟ execution of the function continues  error: an indication that a fatal problem has occurred produced by stop function; execution stops  condition: a generic concept for indicating that something unexpected can occur; programmers can create their own conditions
  • 27. Thanks a lot For Question Read more  dwivedishashwat@gmail.com