SlideShare a Scribd company logo
An overview of R: Text Analytics
Ashraf Uddin
PhD Scholar, Dept. of Computer Science
South Asian University, New Delhi
https://sites.google.com/site/ashrafuddininfo/
About R
What is R?
R is a dialect of the S language.
R is a free software programming language
software environment for statistical computing and graphics
 widely used among statisticians and data miners for
developing statistical software and data analysis
The source code for the R software environment is written primarily
in C, Fortran, and R
History of R
1991: Created by Ross Ihaka and Robert Gentleman at the
University of Auckland, New Zealand
1993: First announcement of R to the public.
1995: R was made as free software.
1997: The R Core Group is formed (containing some people
associated with S-PLUS). The core group controls the source
code for R.
2000: R version 1.0.0 is released.
2013: R version 3.1.2 has been released on 2014-10-31.
Statistical features of R
provides a wide variety of statistical and graphical techniques:
linear and nonlinear modelling
classical statistical tests
 time-series analysis,
classification, clustering
Others
easily extensible through functions and extensions
Many of R's standard functions are written in R itself
C, C++, and Fortran code can be linked and called at run time
strength of R is static graphics
Dynamic and interactive graphics are available through additional
packages
Programming features of R
R is an interpreted language, users typically access it through
a command-line interpreter.
Like other similar languages such as MATLAB, R supports matrix
arithmetic
R supports procedural programming with functions
for some functions, object-oriented programming with generic
functions
Features of R continued...
functionality is divided into modular packages
Graphics capabilities very sophisticated.
Useful for interactive work, but contains a powerful
programming language for developing new tools
Very active and vibrant user community; R-help and R-devel
mailing lists and Stack Overflow
Design of the R System
The R system is divided into 2 conceptual parts:
The “base” R system that you download from CRAN
Everything else.
R functionality is divided into a number of packages
The “base” R system contains, among other things, the base
package which is required to run R and contains the most
fundamental functions.
The other packages contained in the “base” system include utils,
stats, datasets, graphics, grDevices, grid, methods, tools, parallel,
compiler, splines, tcltk, stats4.
There are also other packages: tm, stringr, boot, class, cluster,
codetools, foreign, KernSmooth, lattice, mgcv, nlme, rpart, survival,
MASS, spatial, nnet, Matrix.
Design of the R System continued...
And there are many other packages available:
There are about 4000 packages on CRAN that have been developed
by users and programmers around the world.
Start Working in R
Download & Install R: http://www.r-project.org/
Download & Install R studio: http://www.rstudio.com/products/rstudio/download/,
Wikipedia
Materials:
Chambers (2008). Software for Data Analysis, Springer. (your textbook)
Chambers (1998). Programming with Data, Springer.
Venables & Ripley (2002). Modern Applied Statistics with S, Springer.
Venables & Ripley (2000). S Programming, Springer.
Pinheiro & Bates (2000). Mixed-Effects Models in S and S-PLUS, Springer.
Murrell (2005). R Graphics, Chapman & Hall/CRC Press.·
Springer has a series of books called Use R!.
A longer list of books is at http://www.r-project.org/doc/bib/R-books.html
Course on R: https://www.coursera.org/course/rprog
Data Types and Basic Operations
Objects
R has five basic or “atomic” classes of objects:
1. Character
2. numeric (real numbers)
3. Integer
4. Complex
5. logical (True/False)
The most basic object is a vector
A vector can only contain objects of the same class
BUT: The one exception is a list, which is represented as
a vector but can contain objects of different classes.
Data Types and Basic Operations continued...
Numbers
Numbers in R a generally treated as numeric
a special number Inf which represents infinity; e.g. 1 / 0; Inf can be used
in ordinary calculations; e.g. 1 / Inf is 0
The value NaN represents an undefined value (“not a number”); e.g. 0 /
0; NaN can also be thought of as a missing value
Data Types and Basic Operations continued...
Attributes
R objects can have attributes
names, dimnames
dimensions (e.g. matrices, arrays)
Class
Length
Data Types and Basic Operations continued...
Entering Input
The <- symbol is the assignment operator.
The grammar of the language determines whether an
expression is complete or not.
The # character indicates a comment. Anything to the right of
the # (including the # itself) is ignored.
> x <- 1
> print(x)
[1] 1
> x
[1] 1
> msg<- "hello"
> x <- ## Incomplete expression
Data Types and Basic Operations continued...
Printing
The : operator is used to create integer sequences.
> x <- 1:20
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[16] 16 17 18 19 20
Data Types and Basic Operations continued...
Creating Vectors
The c() function can be used to create vectors of objects.
Using the vector() function
> x <- c(0.5, 0.6) ## numeric
> x <- c(TRUE, FALSE) ## logical
> x <- c(T, F) ## logical
> x <- c("a", "b", "c") ## character
> x <- 9:29 ## integer
> x <- c(1+0i, 2+4i) ## complex
> x <- vector("numeric", length = 10)
> x
[1] 0 0 0 0 0 0 0 0 0 0
Data Types and Basic Operations continued...
Mixing Objects
What about the following?
When different objects are mixed in a vector, coercion occurs
so that every element in the vector is of the same class.
> y <- c(1.7, "a") ## character
> y <- c(TRUE, 2) ## numeric
> y <- c("a", TRUE) ## character
Data Types and Basic Operations continued...
Matrices
Matrices are vectors with a dimension attribute. The dimension attribute
is itself an integer vector of length 2 (nrow, ncol)
> m <- matrix(nrow = 2, ncol = 3)
> m
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
> dim(m)
[1] 2 3
> attributes(m)$dim
[1] 2 3
> m <- matrix(1:6, nrow = 2, ncol = 3)
> m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Data Types and Basic Operations continued...
Matrices: Matrix sum & multiplication
> m<-matrix(data=c(1,0,0,4,4,3), nrow=2,ncol=3)
> n<-matrix(data=c(1,2,3,4,5,6), nrow=2,ncol=3)
> m+n
[,1] [,2] [,3]
[1,] 2 3 9
[2,] 2 8 9
> m*n
[,1] [,2] [,3]
[1,] 1 0 20
[2,] 0 16 18
> m%*%n
Error in m %*% n : non-conformable arguments
>n<-matrix(data=c(1,2,3,4), nrow=2,ncol=2)
>n %*% m
[,1] [,2] [,3]
[1,] 1 12 13
[2,] 2 16 20
Data Types and Basic Operations continued...
Lists
Lists are a special type of vector that can contain elements of different
classes. Lists are a very important data type in R and you should get to
know them well.
> x <- list(1, "a", TRUE, 1 + 4i)
> x
[[1]]
[1] 1
[[2]]
[1] "a"
[[3]]
[1] TRUE
[[4]]
[1] 1+4i
Data Types and Basic Operations continued...
Data Frames
Data frames are used to store tabular data
They are represented as a special type of list where every element of
the list has to have the same length
Each element of the list can be thought of as a column and the length of
each element of the list is the number of rows
Unlike matrices, data frames can store different classes of objects in
each column (just like lists); matrices must have every element be the
same class
Data Types and Basic Operations continued...
Data Frames
Data frames (as csvfile)
> x <- data.frame(foo = 1:4, bar = c(T, T, F, F))
> x
foo bar
1 1 TRUE
2 2 TRUE
3 3 FALSE
4 4 FALSE
> nrow(x)
[1] 4
> ncol(x)
[1] 2
> data<-read.csv("G:/records.csv")
>cd<-data[data$PY==2000,]
>cd<-data[data$PY==2012,]
Reading and Writing Data continued...
Reading Data
There are a few principal functions reading data into R.
read.table, read.csv, for reading tabular data
readLines, for reading lines of a text file
Reading and Writing Data continued...
Writing Data
There are analogous functions for writing data to files
write.table
writeLines
save
Reading and Writing Data continued...
Reading Lines of a Text File
>con <- file("foo.txt", "r")
> x <- readLines(con, 10)
> x
[1] "1080" "10-point" "10th" "11-point"
[5] "12-point" "16-point" "18-point" "1st"
[9] "2" "20-point
## This might take time
>con <- url("http://www.jhsph.edu", "r")
>x <- readLines(con)
> head(x)
[1] "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">"
[2] ""
[3] "<html>"
[4] "<head>"
[5] "t<meta http-equiv="Content-Type" content="text/html;charset=utf-8
Functions
Functions are created using the function() directive and are
stored as R objects just like anything else.
Functions can be passed as arguments to other functions
Functions can be nested
The return value of a function is the last expression in the function body to be
evaluated.
f <- function(<arguments>) {
## Do something interesting
}
Functions continued...
Defining a Function
average<-function(array=numeric(1)){
sum<-0
for(i in 1: length(array)){
sum<-sum+array[i]
}
value<-sum/length(array)
value
}
> m<-c(10,11,2)
> average(m)
[1] 7.666667
> average(10)
[1] 10
> average()
[1] 0
Implementation: Word Frequency
text.files<-
list.files(path="C:/Users/Ashraf/Desktop/txt",full.names = T)
for(fp in text.files){
data<-readLines(con = fp) #read text file line by line
words<-character()
# extract words from each line
for(line in 1: length(data)){
if(data[line]!=""){
list<-unlist(strsplit(data[line]," "))
list<-list[list!=""] #remove the empty strings
words<-c(words,list)
}
}
show(sort(table(words),decreasing = T))
}
Implementation: POS Tagging
## packages NLP, openNLP
library("tm")
library("NLP")
library("openNLP")
## Some text.
data("acq")
s <- as.String(acq[[10]])
## Need sentence and word token annotations.
sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- annotate(s, list(sent_token_annotator,
word_token_annotator))
pos_tag_annotator <- Maxent_POS_Tag_Annotator()
#pos_tag_annotator
a3 <- annotate(s, pos_tag_annotator, a2)
a3w <- subset(a3, type == "word")
tags <- sapply(a3w$features, "[[", "POS")
show(sprintf("%s/%s", s[a3w], tags))
Implementation: Text Classification
Training data set
Test data set
Data set (Training +Test data set)
Example: Sports, News, Opinion/ Reviews
Two basic steps
Representation of text documents (TDM)
Supervised/ Unsupervised algorithm
Implementation: Text Classification
Making TDM (Term Document Matrix):
Making Corpus
Clean Corpus (removing punctuation, stop words, white space, lower case)
Your suggestion is highly appreciated.
Thank You

More Related Content

What's hot

R programming
R programmingR programming
R programming
Nandhini G
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
David Chiu
 
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
Korkrid Akepanidtaworn
 
R language
R languageR language
R programming for data science
R programming for data scienceR programming for data science
R programming for data science
Sovello Hildebrand
 
Class ppt intro to r
Class ppt intro to rClass ppt intro to r
Class ppt intro to r
JigsawAcademy2014
 
R programming Language , Rahul Singh
R programming Language , Rahul SinghR programming Language , Rahul Singh
R programming Language , Rahul Singh
Ravi Basil
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
Nimrita Koul
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
Mahmoud Shiri Varamini
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
Ragia Ibrahim
 
How to get started with R programming
How to get started with R programmingHow to get started with R programming
How to get started with R programming
Ramon Salazar
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
Dr. Awase Khirni Syed
 
2 it unit-1 start learning r
2 it   unit-1 start learning r2 it   unit-1 start learning r
2 it unit-1 start learning r
Netaji Gandi
 
Introduction to statistical software R
Introduction to statistical software RIntroduction to statistical software R
Introduction to statistical software R
Paola Pozzolo - La tua statistica
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics Platform
Syracuse University
 
R tutorial
R tutorialR tutorial
R tutorial
Richard Vidgen
 
The History and Use of R
The History and Use of RThe History and Use of R
The History and Use of R
AnalyticsWeek
 
1 R Tutorial Introduction
1 R Tutorial Introduction1 R Tutorial Introduction
1 R Tutorial Introduction
Sakthi Dasans
 
R programming
R programmingR programming
R programming
Pooja Sharma
 
R Introduction
R IntroductionR Introduction
R Introductionschamber
 

What's hot (20)

R programming
R programmingR programming
R programming
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
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
 
R language
R languageR language
R language
 
R programming for data science
R programming for data scienceR programming for data science
R programming for data science
 
Class ppt intro to r
Class ppt intro to rClass ppt intro to r
Class ppt intro to r
 
R programming Language , Rahul Singh
R programming Language , Rahul SinghR programming Language , Rahul Singh
R programming Language , Rahul Singh
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 
How to get started with R programming
How to get started with R programmingHow to get started with R programming
How to get started with R programming
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
2 it unit-1 start learning r
2 it   unit-1 start learning r2 it   unit-1 start learning r
2 it unit-1 start learning r
 
Introduction to statistical software R
Introduction to statistical software RIntroduction to statistical software R
Introduction to statistical software R
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics Platform
 
R tutorial
R tutorialR tutorial
R tutorial
 
The History and Use of R
The History and Use of RThe History and Use of R
The History and Use of R
 
1 R Tutorial Introduction
1 R Tutorial Introduction1 R Tutorial Introduction
1 R Tutorial Introduction
 
R programming
R programmingR programming
R programming
 
R Introduction
R IntroductionR Introduction
R Introduction
 

Viewers also liked

Dwdm naive bayes_ankit_gadgil_027
Dwdm naive bayes_ankit_gadgil_027Dwdm naive bayes_ankit_gadgil_027
Dwdm naive bayes_ankit_gadgil_027ankitgadgil
 
Risk Classification with an Adaptive Naive Bayes Kernel Machine Model
Risk Classification with an Adaptive Naive Bayes Kernel Machine ModelRisk Classification with an Adaptive Naive Bayes Kernel Machine Model
Risk Classification with an Adaptive Naive Bayes Kernel Machine Model
Jessica Minnier
 
Text classification
Text classificationText classification
Text classification
James Wong
 
OSGeo와 Open Data
OSGeo와 Open DataOSGeo와 Open Data
OSGeo와 Open Data
r-kor
 
황성수 공공데이터 개방과 공공이슈 해결
황성수 공공데이터 개방과 공공이슈 해결황성수 공공데이터 개방과 공공이슈 해결
황성수 공공데이터 개방과 공공이슈 해결
r-kor
 
Deciphering voice of customer through speech analytics
Deciphering voice of customer through speech analyticsDeciphering voice of customer through speech analytics
Deciphering voice of customer through speech analytics
R Systems International
 
Distributed R: The Next Generation Platform for Predictive Analytics
Distributed R: The Next Generation Platform for Predictive AnalyticsDistributed R: The Next Generation Platform for Predictive Analytics
Distributed R: The Next Generation Platform for Predictive Analytics
Jorge Martinez de Salinas
 
Optimizing Facebook Campaigns with R
Optimizing Facebook Campaigns with ROptimizing Facebook Campaigns with R
Optimizing Facebook Campaigns with R
Domino Data Lab
 
The Next List: R&D Breakthroughs that are Changing the World
The Next List: R&D Breakthroughs that are Changing the WorldThe Next List: R&D Breakthroughs that are Changing the World
The Next List: R&D Breakthroughs that are Changing the World
GE
 
Cloud Conf 2015 - Develop and Deploy IOT Applications
Cloud Conf 2015 - Develop and Deploy IOT ApplicationsCloud Conf 2015 - Develop and Deploy IOT Applications
Cloud Conf 2015 - Develop and Deploy IOT Applications
Corley S.r.l.
 
R lecture oga
R lecture ogaR lecture oga
R lecture oga
Osamu Ogasawara
 
Implementing a highly scalable stock prediction system with R, Geode, SpringX...
Implementing a highly scalable stock prediction system with R, Geode, SpringX...Implementing a highly scalable stock prediction system with R, Geode, SpringX...
Implementing a highly scalable stock prediction system with R, Geode, SpringX...
William Markito Oliveira
 
IMCSummit 2015 - Day 2 Developer Track - Implementing a Highly Scalable In-Me...
IMCSummit 2015 - Day 2 Developer Track - Implementing a Highly Scalable In-Me...IMCSummit 2015 - Day 2 Developer Track - Implementing a Highly Scalable In-Me...
IMCSummit 2015 - Day 2 Developer Track - Implementing a Highly Scalable In-Me...
In-Memory Computing Summit
 
오픈데이터와 오픈소스 소프트웨어를 이용한 의료이용정보의 시각화
오픈데이터와 오픈소스 소프트웨어를 이용한 의료이용정보의 시각화오픈데이터와 오픈소스 소프트웨어를 이용한 의료이용정보의 시각화
오픈데이터와 오픈소스 소프트웨어를 이용한 의료이용정보의 시각화
r-kor
 
Trading System Design
Trading System DesignTrading System Design
Trading System Design
Marketcalls
 
구조화된 데이터: Schema.org와 Microdata, RDFa, JSON-LD
구조화된 데이터: Schema.org와 Microdata, RDFa, JSON-LD구조화된 데이터: Schema.org와 Microdata, RDFa, JSON-LD
구조화된 데이터: Schema.org와 Microdata, RDFa, JSON-LD
r-kor
 
Trading sentimental analysis
Trading sentimental analysisTrading sentimental analysis
Trading sentimental analysis
Marketcalls
 
II-SDV 2016 Patrick Beaucamp - Data Science with R and Vanilla Air
II-SDV 2016 Patrick Beaucamp - Data Science with R and Vanilla AirII-SDV 2016 Patrick Beaucamp - Data Science with R and Vanilla Air
II-SDV 2016 Patrick Beaucamp - Data Science with R and Vanilla Air
Dr. Haxel Consult
 
Language R
Language RLanguage R
Language R
Girish Khanzode
 
Taking R Analytics to SQL and the Cloud
Taking R Analytics to SQL and the CloudTaking R Analytics to SQL and the Cloud
Taking R Analytics to SQL and the Cloud
Revolution Analytics
 

Viewers also liked (20)

Dwdm naive bayes_ankit_gadgil_027
Dwdm naive bayes_ankit_gadgil_027Dwdm naive bayes_ankit_gadgil_027
Dwdm naive bayes_ankit_gadgil_027
 
Risk Classification with an Adaptive Naive Bayes Kernel Machine Model
Risk Classification with an Adaptive Naive Bayes Kernel Machine ModelRisk Classification with an Adaptive Naive Bayes Kernel Machine Model
Risk Classification with an Adaptive Naive Bayes Kernel Machine Model
 
Text classification
Text classificationText classification
Text classification
 
OSGeo와 Open Data
OSGeo와 Open DataOSGeo와 Open Data
OSGeo와 Open Data
 
황성수 공공데이터 개방과 공공이슈 해결
황성수 공공데이터 개방과 공공이슈 해결황성수 공공데이터 개방과 공공이슈 해결
황성수 공공데이터 개방과 공공이슈 해결
 
Deciphering voice of customer through speech analytics
Deciphering voice of customer through speech analyticsDeciphering voice of customer through speech analytics
Deciphering voice of customer through speech analytics
 
Distributed R: The Next Generation Platform for Predictive Analytics
Distributed R: The Next Generation Platform for Predictive AnalyticsDistributed R: The Next Generation Platform for Predictive Analytics
Distributed R: The Next Generation Platform for Predictive Analytics
 
Optimizing Facebook Campaigns with R
Optimizing Facebook Campaigns with ROptimizing Facebook Campaigns with R
Optimizing Facebook Campaigns with R
 
The Next List: R&D Breakthroughs that are Changing the World
The Next List: R&D Breakthroughs that are Changing the WorldThe Next List: R&D Breakthroughs that are Changing the World
The Next List: R&D Breakthroughs that are Changing the World
 
Cloud Conf 2015 - Develop and Deploy IOT Applications
Cloud Conf 2015 - Develop and Deploy IOT ApplicationsCloud Conf 2015 - Develop and Deploy IOT Applications
Cloud Conf 2015 - Develop and Deploy IOT Applications
 
R lecture oga
R lecture ogaR lecture oga
R lecture oga
 
Implementing a highly scalable stock prediction system with R, Geode, SpringX...
Implementing a highly scalable stock prediction system with R, Geode, SpringX...Implementing a highly scalable stock prediction system with R, Geode, SpringX...
Implementing a highly scalable stock prediction system with R, Geode, SpringX...
 
IMCSummit 2015 - Day 2 Developer Track - Implementing a Highly Scalable In-Me...
IMCSummit 2015 - Day 2 Developer Track - Implementing a Highly Scalable In-Me...IMCSummit 2015 - Day 2 Developer Track - Implementing a Highly Scalable In-Me...
IMCSummit 2015 - Day 2 Developer Track - Implementing a Highly Scalable In-Me...
 
오픈데이터와 오픈소스 소프트웨어를 이용한 의료이용정보의 시각화
오픈데이터와 오픈소스 소프트웨어를 이용한 의료이용정보의 시각화오픈데이터와 오픈소스 소프트웨어를 이용한 의료이용정보의 시각화
오픈데이터와 오픈소스 소프트웨어를 이용한 의료이용정보의 시각화
 
Trading System Design
Trading System DesignTrading System Design
Trading System Design
 
구조화된 데이터: Schema.org와 Microdata, RDFa, JSON-LD
구조화된 데이터: Schema.org와 Microdata, RDFa, JSON-LD구조화된 데이터: Schema.org와 Microdata, RDFa, JSON-LD
구조화된 데이터: Schema.org와 Microdata, RDFa, JSON-LD
 
Trading sentimental analysis
Trading sentimental analysisTrading sentimental analysis
Trading sentimental analysis
 
II-SDV 2016 Patrick Beaucamp - Data Science with R and Vanilla Air
II-SDV 2016 Patrick Beaucamp - Data Science with R and Vanilla AirII-SDV 2016 Patrick Beaucamp - Data Science with R and Vanilla Air
II-SDV 2016 Patrick Beaucamp - Data Science with R and Vanilla Air
 
Language R
Language RLanguage R
Language R
 
Taking R Analytics to SQL and the Cloud
Taking R Analytics to SQL and the CloudTaking R Analytics to SQL and the Cloud
Taking R Analytics to SQL and the Cloud
 

Similar to A short tutorial on r

R basics
R basicsR basics
R basics
Sagun Baijal
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
vikassingh569137
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MD
SonaCharles2
 
17641.ppt
17641.ppt17641.ppt
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
Unmesh Baile
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
ranapoonam1
 
Lecture_R.ppt
Lecture_R.pptLecture_R.ppt
Lecture_R.ppt
Abebe334138
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
ArchishaKhandareSS20
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
vikassingh569137
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
anshikagoel52
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
NareshKarela1
 
Unit 3
Unit 3Unit 3
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptx
rajalakshmi5921
 
How to obtain and install R.ppt
How to obtain and install R.pptHow to obtain and install R.ppt
How to obtain and install R.ppt
rajalakshmi5921
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R lang
senthil0809
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
DrGSakthiGovindaraju
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
attalurilalitha
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
AmanBhalla14
 

Similar to A short tutorial on r (20)

R basics
R basicsR basics
R basics
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MD
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
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
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
Lecture_R.ppt
Lecture_R.pptLecture_R.ppt
Lecture_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
Unit 3
Unit 3Unit 3
Unit 3
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptx
 
How to obtain and install R.ppt
How to obtain and install R.pptHow to obtain and install R.ppt
How to obtain and install R.ppt
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R lang
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 

More from Ashraf Uddin

Big Data: Its Characteristics And Architecture Capabilities
Big Data: Its Characteristics And Architecture CapabilitiesBig Data: Its Characteristics And Architecture Capabilities
Big Data: Its Characteristics And Architecture Capabilities
Ashraf Uddin
 
MapReduce: Simplified Data Processing on Large Clusters
MapReduce: Simplified Data Processing on Large ClustersMapReduce: Simplified Data Processing on Large Clusters
MapReduce: Simplified Data Processing on Large Clusters
Ashraf Uddin
 
Text Mining Infrastructure in R
Text Mining Infrastructure in RText Mining Infrastructure in R
Text Mining Infrastructure in R
Ashraf Uddin
 
Dynamic source routing
Dynamic source routingDynamic source routing
Dynamic source routingAshraf Uddin
 

More from Ashraf Uddin (7)

Big Data: Its Characteristics And Architecture Capabilities
Big Data: Its Characteristics And Architecture CapabilitiesBig Data: Its Characteristics And Architecture Capabilities
Big Data: Its Characteristics And Architecture Capabilities
 
MapReduce: Simplified Data Processing on Large Clusters
MapReduce: Simplified Data Processing on Large ClustersMapReduce: Simplified Data Processing on Large Clusters
MapReduce: Simplified Data Processing on Large Clusters
 
Text Mining Infrastructure in R
Text Mining Infrastructure in RText Mining Infrastructure in R
Text Mining Infrastructure in R
 
Software piracy
Software piracySoftware piracy
Software piracy
 
Naive bayes
Naive bayesNaive bayes
Naive bayes
 
Freenet
FreenetFreenet
Freenet
 
Dynamic source routing
Dynamic source routingDynamic source routing
Dynamic source routing
 

Recently uploaded

一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Linda486226
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 

Recently uploaded (20)

一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 

A short tutorial on r

  • 1. An overview of R: Text Analytics Ashraf Uddin PhD Scholar, Dept. of Computer Science South Asian University, New Delhi https://sites.google.com/site/ashrafuddininfo/
  • 2. About R What is R? R is a dialect of the S language. R is a free software programming language software environment for statistical computing and graphics  widely used among statisticians and data miners for developing statistical software and data analysis The source code for the R software environment is written primarily in C, Fortran, and R
  • 3. History of R 1991: Created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand 1993: First announcement of R to the public. 1995: R was made as free software. 1997: The R Core Group is formed (containing some people associated with S-PLUS). The core group controls the source code for R. 2000: R version 1.0.0 is released. 2013: R version 3.1.2 has been released on 2014-10-31.
  • 4. Statistical features of R provides a wide variety of statistical and graphical techniques: linear and nonlinear modelling classical statistical tests  time-series analysis, classification, clustering Others easily extensible through functions and extensions Many of R's standard functions are written in R itself C, C++, and Fortran code can be linked and called at run time strength of R is static graphics Dynamic and interactive graphics are available through additional packages
  • 5. Programming features of R R is an interpreted language, users typically access it through a command-line interpreter. Like other similar languages such as MATLAB, R supports matrix arithmetic R supports procedural programming with functions for some functions, object-oriented programming with generic functions
  • 6. Features of R continued... functionality is divided into modular packages Graphics capabilities very sophisticated. Useful for interactive work, but contains a powerful programming language for developing new tools Very active and vibrant user community; R-help and R-devel mailing lists and Stack Overflow
  • 7. Design of the R System The R system is divided into 2 conceptual parts: The “base” R system that you download from CRAN Everything else. R functionality is divided into a number of packages The “base” R system contains, among other things, the base package which is required to run R and contains the most fundamental functions. The other packages contained in the “base” system include utils, stats, datasets, graphics, grDevices, grid, methods, tools, parallel, compiler, splines, tcltk, stats4. There are also other packages: tm, stringr, boot, class, cluster, codetools, foreign, KernSmooth, lattice, mgcv, nlme, rpart, survival, MASS, spatial, nnet, Matrix.
  • 8. Design of the R System continued... And there are many other packages available: There are about 4000 packages on CRAN that have been developed by users and programmers around the world.
  • 9. Start Working in R Download & Install R: http://www.r-project.org/ Download & Install R studio: http://www.rstudio.com/products/rstudio/download/, Wikipedia Materials: Chambers (2008). Software for Data Analysis, Springer. (your textbook) Chambers (1998). Programming with Data, Springer. Venables & Ripley (2002). Modern Applied Statistics with S, Springer. Venables & Ripley (2000). S Programming, Springer. Pinheiro & Bates (2000). Mixed-Effects Models in S and S-PLUS, Springer. Murrell (2005). R Graphics, Chapman & Hall/CRC Press.· Springer has a series of books called Use R!. A longer list of books is at http://www.r-project.org/doc/bib/R-books.html Course on R: https://www.coursera.org/course/rprog
  • 10. Data Types and Basic Operations Objects R has five basic or “atomic” classes of objects: 1. Character 2. numeric (real numbers) 3. Integer 4. Complex 5. logical (True/False) The most basic object is a vector A vector can only contain objects of the same class BUT: The one exception is a list, which is represented as a vector but can contain objects of different classes.
  • 11. Data Types and Basic Operations continued... Numbers Numbers in R a generally treated as numeric a special number Inf which represents infinity; e.g. 1 / 0; Inf can be used in ordinary calculations; e.g. 1 / Inf is 0 The value NaN represents an undefined value (“not a number”); e.g. 0 / 0; NaN can also be thought of as a missing value
  • 12. Data Types and Basic Operations continued... Attributes R objects can have attributes names, dimnames dimensions (e.g. matrices, arrays) Class Length
  • 13. Data Types and Basic Operations continued... Entering Input The <- symbol is the assignment operator. The grammar of the language determines whether an expression is complete or not. The # character indicates a comment. Anything to the right of the # (including the # itself) is ignored. > x <- 1 > print(x) [1] 1 > x [1] 1 > msg<- "hello" > x <- ## Incomplete expression
  • 14. Data Types and Basic Operations continued... Printing The : operator is used to create integer sequences. > x <- 1:20 > x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [16] 16 17 18 19 20
  • 15. Data Types and Basic Operations continued... Creating Vectors The c() function can be used to create vectors of objects. Using the vector() function > x <- c(0.5, 0.6) ## numeric > x <- c(TRUE, FALSE) ## logical > x <- c(T, F) ## logical > x <- c("a", "b", "c") ## character > x <- 9:29 ## integer > x <- c(1+0i, 2+4i) ## complex > x <- vector("numeric", length = 10) > x [1] 0 0 0 0 0 0 0 0 0 0
  • 16. Data Types and Basic Operations continued... Mixing Objects What about the following? When different objects are mixed in a vector, coercion occurs so that every element in the vector is of the same class. > y <- c(1.7, "a") ## character > y <- c(TRUE, 2) ## numeric > y <- c("a", TRUE) ## character
  • 17. Data Types and Basic Operations continued... Matrices Matrices are vectors with a dimension attribute. The dimension attribute is itself an integer vector of length 2 (nrow, ncol) > m <- matrix(nrow = 2, ncol = 3) > m [,1] [,2] [,3] [1,] NA NA NA [2,] NA NA NA > dim(m) [1] 2 3 > attributes(m)$dim [1] 2 3 > m <- matrix(1:6, nrow = 2, ncol = 3) > m [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6
  • 18. Data Types and Basic Operations continued... Matrices: Matrix sum & multiplication > m<-matrix(data=c(1,0,0,4,4,3), nrow=2,ncol=3) > n<-matrix(data=c(1,2,3,4,5,6), nrow=2,ncol=3) > m+n [,1] [,2] [,3] [1,] 2 3 9 [2,] 2 8 9 > m*n [,1] [,2] [,3] [1,] 1 0 20 [2,] 0 16 18 > m%*%n Error in m %*% n : non-conformable arguments >n<-matrix(data=c(1,2,3,4), nrow=2,ncol=2) >n %*% m [,1] [,2] [,3] [1,] 1 12 13 [2,] 2 16 20
  • 19. Data Types and Basic Operations continued... Lists Lists are a special type of vector that can contain elements of different classes. Lists are a very important data type in R and you should get to know them well. > x <- list(1, "a", TRUE, 1 + 4i) > x [[1]] [1] 1 [[2]] [1] "a" [[3]] [1] TRUE [[4]] [1] 1+4i
  • 20. Data Types and Basic Operations continued... Data Frames Data frames are used to store tabular data They are represented as a special type of list where every element of the list has to have the same length Each element of the list can be thought of as a column and the length of each element of the list is the number of rows Unlike matrices, data frames can store different classes of objects in each column (just like lists); matrices must have every element be the same class
  • 21. Data Types and Basic Operations continued... Data Frames Data frames (as csvfile) > x <- data.frame(foo = 1:4, bar = c(T, T, F, F)) > x foo bar 1 1 TRUE 2 2 TRUE 3 3 FALSE 4 4 FALSE > nrow(x) [1] 4 > ncol(x) [1] 2 > data<-read.csv("G:/records.csv") >cd<-data[data$PY==2000,] >cd<-data[data$PY==2012,]
  • 22. Reading and Writing Data continued... Reading Data There are a few principal functions reading data into R. read.table, read.csv, for reading tabular data readLines, for reading lines of a text file
  • 23. Reading and Writing Data continued... Writing Data There are analogous functions for writing data to files write.table writeLines save
  • 24. Reading and Writing Data continued... Reading Lines of a Text File >con <- file("foo.txt", "r") > x <- readLines(con, 10) > x [1] "1080" "10-point" "10th" "11-point" [5] "12-point" "16-point" "18-point" "1st" [9] "2" "20-point ## This might take time >con <- url("http://www.jhsph.edu", "r") >x <- readLines(con) > head(x) [1] "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">" [2] "" [3] "<html>" [4] "<head>" [5] "t<meta http-equiv="Content-Type" content="text/html;charset=utf-8
  • 25. Functions Functions are created using the function() directive and are stored as R objects just like anything else. Functions can be passed as arguments to other functions Functions can be nested The return value of a function is the last expression in the function body to be evaluated. f <- function(<arguments>) { ## Do something interesting }
  • 26. Functions continued... Defining a Function average<-function(array=numeric(1)){ sum<-0 for(i in 1: length(array)){ sum<-sum+array[i] } value<-sum/length(array) value } > m<-c(10,11,2) > average(m) [1] 7.666667 > average(10) [1] 10 > average() [1] 0
  • 27. Implementation: Word Frequency text.files<- list.files(path="C:/Users/Ashraf/Desktop/txt",full.names = T) for(fp in text.files){ data<-readLines(con = fp) #read text file line by line words<-character() # extract words from each line for(line in 1: length(data)){ if(data[line]!=""){ list<-unlist(strsplit(data[line]," ")) list<-list[list!=""] #remove the empty strings words<-c(words,list) } } show(sort(table(words),decreasing = T)) }
  • 28. Implementation: POS Tagging ## packages NLP, openNLP library("tm") library("NLP") library("openNLP") ## Some text. data("acq") s <- as.String(acq[[10]]) ## Need sentence and word token annotations. sent_token_annotator <- Maxent_Sent_Token_Annotator() word_token_annotator <- Maxent_Word_Token_Annotator() a2 <- annotate(s, list(sent_token_annotator, word_token_annotator)) pos_tag_annotator <- Maxent_POS_Tag_Annotator() #pos_tag_annotator a3 <- annotate(s, pos_tag_annotator, a2) a3w <- subset(a3, type == "word") tags <- sapply(a3w$features, "[[", "POS") show(sprintf("%s/%s", s[a3w], tags))
  • 29. Implementation: Text Classification Training data set Test data set Data set (Training +Test data set) Example: Sports, News, Opinion/ Reviews Two basic steps Representation of text documents (TDM) Supervised/ Unsupervised algorithm
  • 30. Implementation: Text Classification Making TDM (Term Document Matrix): Making Corpus Clean Corpus (removing punctuation, stop words, white space, lower case)
  • 31. Your suggestion is highly appreciated. Thank You