0% found this document useful (0 votes)
15 views377 pages

Data Analysis Challenges with R

The document discusses statistical programming with R and outlines various challenges in data analysis such as presenting information accessibly, exploding data, and rapidly growing technologies. It then summarizes the stages of data analysis including importing and cleaning data, data visualization, fitting statistical models, and reporting results. Finally, it provides reasons for using R such as it being free, frequently updated, and having excellent graphics and data import capabilities.

Uploaded by

mahesh.nannepagu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views377 pages

Data Analysis Challenges with R

The document discusses statistical programming with R and outlines various challenges in data analysis such as presenting information accessibly, exploding data, and rapidly growing technologies. It then summarizes the stages of data analysis including importing and cleaning data, data visualization, fitting statistical models, and reporting results. Finally, it provides reasons for using R such as it being free, frequently updated, and having excellent graphics and data import capabilities.

Uploaded by

mahesh.nannepagu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Statistical Programming with R

[Link]

Professor of Mathematics
J N T University Kakinada.

email: nath_vr@[Link]

VRNath (JNTU KAKINADA) UG-STAT 1 / 34


Challenges in Data Analysis

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.


2 Explosion of Data

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.


2 Explosion of Data
3 Fast Growing of Technologies

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.


2 Explosion of Data
3 Fast Growing of Technologies
4 Before personal computers and the internet

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.


2 Explosion of Data
3 Fast Growing of Technologies
4 Before personal computers and the internet
5 Now New methodologies appear daily.

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.


2 Explosion of Data
3 Fast Growing of Technologies
4 Before personal computers and the internet
5 Now New methodologies appear daily.
6 Easy Access To Computing

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.


2 Explosion of Data
3 Fast Growing of Technologies
4 Before personal computers and the internet
5 Now New methodologies appear daily.
6 Easy Access To ComputingWith the cheap and easy access
afforded by personal computers

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.


2 Explosion of Data
3 Fast Growing of Technologies
4 Before personal computers and the internet
5 Now New methodologies appear daily.
6 Easy Access To ComputingWith the cheap and easy access
afforded by personal computers
7 Interactive Computing

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Challenges in Data Analysis

1 Presenting information in easily accessible and digestible ways.


2 Explosion of Data
3 Fast Growing of Technologies
4 Before personal computers and the internet
5 Now New methodologies appear daily.
6 Easy Access To ComputingWith the cheap and easy access
afforded by personal computers
7 Interactive Computing

VRNath (JNTU KAKINADA) UG-STAT 2 / 34


Various Stages of Data Analysis

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization
4 Fit a statistical model

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization
4 Fit a statistical model
1 Cross-validate the model

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization
4 Fit a statistical model
1 Cross-validate the model
2 Evaluate the model fit

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization
4 Fit a statistical model
1 Cross-validate the model
2 Evaluate the model fit
5 Evaluate model prediction on new data

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization
4 Fit a statistical model
1 Cross-validate the model
2 Evaluate the model fit
5 Evaluate model prediction on new data
6 Produce report

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization
4 Fit a statistical model
1 Cross-validate the model
2 Evaluate the model fit
5 Evaluate model prediction on new data
6 Produce report

The R Package

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization
4 Fit a statistical model
1 Cross-validate the model
2 Evaluate the model fit
5 Evaluate model prediction on new data
6 Produce report

The R Package
R is a comprehensive software package ideally suited to accomplish
these goals

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Various Stages of Data Analysis

1 Import Data
2 Prepare, explore, and clean data
3 Data Visualization
4 Fit a statistical model
1 Cross-validate the model
2 Evaluate the model fit
5 Evaluate model prediction on new data
6 Produce report

The R Package
R is a comprehensive software package ideally suited to accomplish
these goals

VRNath (JNTU KAKINADA) UG-STAT 3 / 34


Why we use R?

1 R is open source and FREE

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Why we use R?

1 R is open source and FREE


2 Updated very frequently

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Why we use R?

1 R is open source and FREE


2 Updated very frequently
3 Highly interactive

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Why we use R?

1 R is open source and FREE


2 Updated very frequently
3 Highly interactive
4 Data Import

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Why we use R?

1 R is open source and FREE


2 Updated very frequently
3 Highly interactive
4 Data Import
5 New Statistical Methods

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Why we use R?

1 R is open source and FREE


2 Updated very frequently
3 Highly interactive
4 Data Import
5 New Statistical Methods
6 Excellent File Sharing System

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Why we use R?

1 R is open source and FREE


2 Updated very frequently
3 Highly interactive
4 Data Import
5 New Statistical Methods
6 Excellent File Sharing System
7 Excellent GUI

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Why we use R?

1 R is open source and FREE


2 Updated very frequently
3 Highly interactive
4 Data Import
5 New Statistical Methods
6 Excellent File Sharing System
7 Excellent GUI
8 Easily create source

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Why we use R?

1 R is open source and FREE


2 Updated very frequently
3 Highly interactive
4 Data Import
5 New Statistical Methods
6 Excellent File Sharing System
7 Excellent GUI
8 Easily create source

VRNath (JNTU KAKINADA) UG-STAT 4 / 34


Obtaining and installing R

VRNath (JNTU KAKINADA) UG-STAT 5 / 34


Obtaining and installing R

VRNath (JNTU KAKINADA) UG-STAT 5 / 34


Obtaining and installing R

1 R is a language and environment for statistical computing and


graphics

VRNath (JNTU KAKINADA) UG-STAT 5 / 34


Obtaining and installing R

1 R is a language and environment for statistical computing and


graphics
2 The commercial version of R is S language originally developed at
Bell Labs

VRNath (JNTU KAKINADA) UG-STAT 5 / 34


Obtaining and installing R

1 R is a language and environment for statistical computing and


graphics
2 The commercial version of R is S language originally developed at
Bell Labs

Download Free

VRNath (JNTU KAKINADA) UG-STAT 5 / 34


Obtaining and installing R

1 R is a language and environment for statistical computing and


graphics
2 The commercial version of R is S language originally developed at
Bell Labs

Download Free
Available from the Comprehensive R Archive Network (CRAN) at
http:// [Link]

VRNath (JNTU KAKINADA) UG-STAT 5 / 34


Obtaining and installing R

1 R is a language and environment for statistical computing and


graphics
2 The commercial version of R is S language originally developed at
Bell Labs

Download Free
Available from the Comprehensive R Archive Network (CRAN) at
http:// [Link]

R Studio
R is accompanied by a user interface R Studio can be downloaded at
[Link]

VRNath (JNTU KAKINADA) UG-STAT 5 / 34


Working with R

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.


2 Enter commands one at a time at the command prompt (>)

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.


2 Enter commands one at a time at the command prompt (>)
or

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.


2 Enter commands one at a time at the command prompt (>)
or
3 Run a set of commands from a source file.

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.


2 Enter commands one at a time at the command prompt (>)
or
3 Run a set of commands from a source file.
4 Most functionality is created through built-in and user-created
functions known as objects.

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.


2 Enter commands one at a time at the command prompt (>)
or
3 Run a set of commands from a source file.
4 Most functionality is created through built-in and user-created
functions known as objects.

objects

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.


2 Enter commands one at a time at the command prompt (>)
or
3 Run a set of commands from a source file.
4 Most functionality is created through built-in and user-created
functions known as objects.

objects
An object is basically anything that can be assigned a value

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.


2 Enter commands one at a time at the command prompt (>)
or
3 Run a set of commands from a source file.
4 Most functionality is created through built-in and user-created
functions known as objects.

objects
An object is basically anything that can be assigned a value (data,
functions, graphs, analytic results, and more)

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Working with R

1 R is a case-sensitive, interpreted language.


2 Enter commands one at a time at the command prompt (>)
or
3 Run a set of commands from a source file.
4 Most functionality is created through built-in and user-created
functions known as objects.

objects
An object is basically anything that can be assigned a value (data,
functions, graphs, analytic results, and more)

VRNath (JNTU KAKINADA) UG-STAT 6 / 34


Creating the Objects

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)
[1]7.06

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)
[1]7.06
> sd(weight)

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)
[1]7.06
> sd(weight)
[1]2.077498

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)
[1]7.06
> sd(weight)
[1]2.077498
> cor(age,weight)

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)
[1]7.06
> sd(weight)
[1]2.077498
> cor(age,weight)
[1]0.9075655

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)
[1]7.06
> sd(weight)
[1]2.077498
> cor(age,weight)
[1]0.9075655
> plot(age,weight)

VRNath (JNTU KAKINADA) UG-STAT 7 / 34


Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)
[1]7.06
> sd(weight)
[1]2.077498
> cor(age,weight)
[1]0.9075655
> plot(age,weight)
> q()
VRNath (JNTU KAKINADA) UG-STAT 7 / 34
Creating the Objects
1 x<-rnorm(5) creates an object x. It is same as rnorm(5) -> x
2 Comments are preceded by the # symbol
3 The q() function ends the session and lets you quit
A Typical R Program
> age <- c(1,3,5,2,11,9,3,9,12,3)
> weight <-
c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> mean(weight)
[1]7.06
> sd(weight)
[1]2.077498
> cor(age,weight)
[1]0.9075655
> plot(age,weight)
> q()
VRNath (JNTU KAKINADA) UG-STAT 7 / 34
The help functions in R

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the
example("foo")

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the
example("foo")
apropos("foo", mode="function")

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the
example("foo")
apropos("foo", mode="function")
data()

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the
example("foo")
apropos("foo", mode="function")
data()

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the
example("foo")
apropos("foo", mode="function")
data()

The R workspace

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the
example("foo")
apropos("foo", mode="function")
data()

The R workspace
1 Current R working environment and any user-defined objects
(vectors, matrices, functions, data frames, and lists)

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the
example("foo")
apropos("foo", mode="function")
data()

The R workspace
1 Current R working environment and any user-defined objects
(vectors, matrices, functions, data frames, and lists)
2 We can save an image of the current workspace automatically
reloaded the next time R starts

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


The help functions in R

R Function
[Link]()
help("foo") or ?foo
[Link]("foo") or ??foo Searches the help system for instances of the
example("foo")
apropos("foo", mode="function")
data()

The R workspace
1 Current R working environment and any user-defined objects
(vectors, matrices, functions, data frames, and lists)
2 We can save an image of the current workspace automatically
reloaded the next time R starts

VRNath (JNTU KAKINADA) UG-STAT 8 / 34


Organizing your work in R

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace
rm(objectlist) Removes (deletes) one or more objects

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace
rm(objectlist) Removes (deletes) one or more objects
history(#) Displays your last # commands (default = 25)

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace
rm(objectlist) Removes (deletes) one or more objects
history(#) Displays your last # commands (default = 25)
savehistory("myfile") Saves commands history (def = .Rhistory)

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace
rm(objectlist) Removes (deletes) one or more objects
history(#) Displays your last # commands (default = 25)
savehistory("myfile") Saves commands history (def = .Rhistory)
loadhistory("myfile") Reloads a commands history (def = .Rhistory)

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace
rm(objectlist) Removes (deletes) one or more objects
history(#) Displays your last # commands (default = 25)
savehistory("myfile") Saves commands history (def = .Rhistory)
loadhistory("myfile") Reloads a commands history (def = .Rhistory)
[Link]("myfile") Saves the workspace to myfile (def = .RData)

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace
rm(objectlist) Removes (deletes) one or more objects
history(#) Displays your last # commands (default = 25)
savehistory("myfile") Saves commands history (def = .Rhistory)
loadhistory("myfile") Reloads a commands history (def = .Rhistory)
[Link]("myfile") Saves the workspace to myfile (def = .RData)
load("myfile") Loads a workspace into the current session

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace
rm(objectlist) Removes (deletes) one or more objects
history(#) Displays your last # commands (default = 25)
savehistory("myfile") Saves commands history (def = .Rhistory)
loadhistory("myfile") Reloads a commands history (def = .Rhistory)
[Link]("myfile") Saves the workspace to myfile (def = .RData)
load("myfile") Loads a workspace into the current session
q() Quits R. You will be prompted to save the workspace

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Organizing your work in R

Table: Functions for managing the R workspace

getwd() Path to Present Working Directory


setwd() Sets path to new working directory
ls() Lists the objects in the current workspace
rm(objectlist) Removes (deletes) one or more objects
history(#) Displays your last # commands (default = 25)
savehistory("myfile") Saves commands history (def = .Rhistory)
loadhistory("myfile") Reloads a commands history (def = .Rhistory)
[Link]("myfile") Saves the workspace to myfile (def = .RData)
load("myfile") Loads a workspace into the current session
q() Quits R. You will be prompted to save the workspace

VRNath (JNTU KAKINADA) UG-STAT 9 / 34


Sample Code for R Work Space

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)
hist(x)
# save the source as myscript.R

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)
hist(x)
# save the source as myscript.R q()

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)
hist(x)
# save the source as myscript.R q()

File Input and Output

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)
hist(x)
# save the source as myscript.R q()

File Input and Output


source("myscript.R") runs a set of commands

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)
hist(x)
# save the source as myscript.R q()

File Input and Output


source("myscript.R") runs a set of commands
sink("filename") saves output

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)
hist(x)
# save the source as myscript.R q()

File Input and Output


source("myscript.R") runs a set of commands
sink("filename") saves output
bmp("[Link]") BMP file

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)
hist(x)
# save the source as myscript.R q()

File Input and Output


source("myscript.R") runs a set of commands
sink("filename") saves output
bmp("[Link]") BMP file
jpeg("[Link]") JPEG file

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


Sample Code for R Work Space

Project Work
setwd("C:/myprojects/project1")
options()
options(digits=3)
x <- runif(20)
summary(x)
hist(x)
# save the source as myscript.R q()

File Input and Output


source("myscript.R") runs a set of commands
sink("filename") saves output
bmp("[Link]") BMP file
jpeg("[Link]") JPEG file

VRNath (JNTU KAKINADA) UG-STAT 10 / 34


The R packages

VRNath (JNTU KAKINADA) UG-STAT 11 / 34


The R packages

VRNath (JNTU KAKINADA) UG-STAT 11 / 34


The R packages

1 Packages are collections of R functions, data, and compiled code


in a well-defined format.

VRNath (JNTU KAKINADA) UG-STAT 11 / 34


The R packages

1 Packages are collections of R functions, data, and compiled code


in a well-defined format.
2 The directory where packages are stored on your computer is
called the library.

VRNath (JNTU KAKINADA) UG-STAT 11 / 34


The R packages

1 Packages are collections of R functions, data, and compiled code


in a well-defined format.
2 The directory where packages are stored on your computer is
called the library.
3 The function .libPaths() shows you where your library is located.

VRNath (JNTU KAKINADA) UG-STAT 11 / 34


The R packages

1 Packages are collections of R functions, data, and compiled code


in a well-defined format.
2 The directory where packages are stored on your computer is
called the library.
3 The function .libPaths() shows you where your library is located.
4 library() shows you what packages you’ve saved in your library.

VRNath (JNTU KAKINADA) UG-STAT 11 / 34


The R packages

1 Packages are collections of R functions, data, and compiled code


in a well-defined format.
2 The directory where packages are stored on your computer is
called the library.
3 The function .libPaths() shows you where your library is located.
4 library() shows you what packages you’ve saved in your library.
5 Installing a package The [Link]() command.

VRNath (JNTU KAKINADA) UG-STAT 11 / 34


The R packages

1 Packages are collections of R functions, data, and compiled code


in a well-defined format.
2 The directory where packages are stored on your computer is
called the library.
3 The function .libPaths() shows you where your library is located.
4 library() shows you what packages you’ve saved in your library.
5 Installing a package The [Link]() command.
6 Installing a package downloads it from a CRAN mirror site and
places it in your library.

VRNath (JNTU KAKINADA) UG-STAT 11 / 34


Loading and Learning the Packages

VRNath (JNTU KAKINADA) UG-STAT 12 / 34


Loading and Learning the Packages

library() To view the files. In not available and


install it.

VRNath (JNTU KAKINADA) UG-STAT 12 / 34


Loading and Learning the Packages

library() To view the files. In not available and


install it.
library(gclus) To load a package named gclus

VRNath (JNTU KAKINADA) UG-STAT 12 / 34


Loading and Learning the Packages

library() To view the files. In not available and


install it.
library(gclus) To load a package named gclus
help(package="package_name") To get the help with
examples, datasets, sample code.

VRNath (JNTU KAKINADA) UG-STAT 12 / 34


Loading and Learning the Packages

library() To view the files. In not available and


install it.
library(gclus) To load a package named gclus
help(package="package_name") To get the help with
examples, datasets, sample code.
Sample Code

VRNath (JNTU KAKINADA) UG-STAT 12 / 34


Loading and Learning the Packages

library() To view the files. In not available and


install it.
library(gclus) To load a package named gclus
help(package="package_name") To get the help with
examples, datasets, sample code.
Sample Code
library(MASS)

VRNath (JNTU KAKINADA) UG-STAT 12 / 34


Loading and Learning the Packages

library() To view the files. In not available and


install it.
library(gclus) To load a package named gclus
help(package="package_name") To get the help with
examples, datasets, sample code.
Sample Code
library(MASS)
help(package=MASS)

VRNath (JNTU KAKINADA) UG-STAT 12 / 34


Loading and Learning the Packages

library() To view the files. In not available and


install it.
library(gclus) To load a package named gclus
help(package="package_name") To get the help with
examples, datasets, sample code.
Sample Code
library(MASS)
help(package=MASS)

VRNath (JNTU KAKINADA) UG-STAT 12 / 34


Understanding datasets

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


Understanding datasets

A dataset is usually a rectangular array of data with rows representing


observations and columns representing variables.

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


Understanding datasets

A dataset is usually a rectangular array of data with rows representing


observations and columns representing variables.

Table: A patient dataset

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


Understanding datasets

A dataset is usually a rectangular array of data with rows representing


observations and columns representing variables.

Table: A patient dataset


PatientID AdmDate Age Diabetes Status

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


Understanding datasets

A dataset is usually a rectangular array of data with rows representing


observations and columns representing variables.

Table: A patient dataset


PatientID AdmDate Age Diabetes Status
1 10/15/2014 25 Type1 Poor

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


Understanding datasets

A dataset is usually a rectangular array of data with rows representing


observations and columns representing variables.

Table: A patient dataset


PatientID AdmDate Age Diabetes Status
1 10/15/2014 25 Type1 Poor
2 11/01/2014 34 Type2 Improved

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


Understanding datasets

A dataset is usually a rectangular array of data with rows representing


observations and columns representing variables.

Table: A patient dataset


PatientID AdmDate Age Diabetes Status
1 10/15/2014 25 Type1 Poor
2 11/01/2014 34 Type2 Improved
3 10/21/2014 28 Type1 Excellent

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


Understanding datasets

A dataset is usually a rectangular array of data with rows representing


observations and columns representing variables.

Table: A patient dataset


PatientID AdmDate Age Diabetes Status
1 10/15/2014 25 Type1 Poor
2 11/01/2014 34 Type2 Improved
3 10/21/2014 28 Type1 Excellent
4 10/28/2014 52 Type1 Poor

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


Understanding datasets

A dataset is usually a rectangular array of data with rows representing


observations and columns representing variables.

Table: A patient dataset


PatientID AdmDate Age Diabetes Status
1 10/15/2014 25 Type1 Poor
2 11/01/2014 34 Type2 Improved
3 10/21/2014 28 Type1 Excellent
4 10/28/2014 52 Type1 Poor

VRNath (JNTU KAKINADA) UG-STAT 13 / 34


The R Data Structures
Scalars

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

Scalar Data

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

Scalar Data
x<-250

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

Scalar Data
x<-250
y<-"Student Name"

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

Scalar Data
x<-250
y<-"Student Name"
buy<-TRUE

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

Scalar Data
x<-250
y<-"Student Name"
buy<-TRUE
object(x,250)

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

Scalar Data
x<-250
y<-"Student Name"
buy<-TRUE
object(x,250)

The assignment can be <- or -> or = or


object(objectName,objectValue)

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Scalars

1 R has a wide variety of objects for holding data


2 scalars, vectors, matrices, arrays, data frames, lists
3 We can fill the data with numeric, nominal, ordinal, logical type.

Scalar Data
x<-250
y<-"Student Name"
buy<-TRUE
object(x,250)

The assignment can be <- or -> or = or


object(objectName,objectValue)

VRNath (JNTU KAKINADA) UG-STAT 14 / 34


The R Data Structures
Vectors

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


The R Data Structures
Vectors

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


The R Data Structures
Vectors

1 Vectors are one-dimensional arrays that can hold numeric data,


character data, or logical data.

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


The R Data Structures
Vectors

1 Vectors are one-dimensional arrays that can hold numeric data,


character data, or logical data.
2 The combine function c() is used to form the vector.

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


The R Data Structures
Vectors

1 Vectors are one-dimensional arrays that can hold numeric data,


character data, or logical data.
2 The combine function c() is used to form the vector.
3 Some examples

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


The R Data Structures
Vectors

1 Vectors are one-dimensional arrays that can hold numeric data,


character data, or logical data.
2 The combine function c() is used to form the vector.
3 Some examples a <- c(1, 2, 5, 3, 6, -2, 4)

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


The R Data Structures
Vectors

1 Vectors are one-dimensional arrays that can hold numeric data,


character data, or logical data.
2 The combine function c() is used to form the vector.
3 Some examples a <- c(1, 2, 5, 3, 6, -2, 4)
b <- c("one", "two", "three")

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


The R Data Structures
Vectors

1 Vectors are one-dimensional arrays that can hold numeric data,


character data, or logical data.
2 The combine function c() is used to form the vector.
3 Some examples a <- c(1, 2, 5, 3, 6, -2, 4)
b <- c("one", "two", "three")
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


The R Data Structures
Vectors

1 Vectors are one-dimensional arrays that can hold numeric data,


character data, or logical data.
2 The combine function c() is used to form the vector.
3 Some examples a <- c(1, 2, 5, 3, 6, -2, 4)
b <- c("one", "two", "three")
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)

VRNath (JNTU KAKINADA) UG-STAT 15 / 34


Matrices

VRNath (JNTU KAKINADA) UG-STAT 16 / 34


Matrices

VRNath (JNTU KAKINADA) UG-STAT 16 / 34


Matrices

A matrix is a two-dimensional array

VRNath (JNTU KAKINADA) UG-STAT 16 / 34


Matrices

A matrix is a two-dimensional array


Each element should be of same mode (numeric, character, or
logical)

VRNath (JNTU KAKINADA) UG-STAT 16 / 34


Matrices

A matrix is a two-dimensional array


Each element should be of same mode (numeric, character, or
logical)
Matrices are created with the matrix() function.

VRNath (JNTU KAKINADA) UG-STAT 16 / 34


Matrices

A matrix is a two-dimensional array


Each element should be of same mode (numeric, character, or
logical)
Matrices are created with the matrix() function.

General R Command

VRNath (JNTU KAKINADA) UG-STAT 16 / 34


Matrices

A matrix is a two-dimensional array


Each element should be of same mode (numeric, character, or
logical)
Matrices are created with the matrix() function.

General R Command
mymatrix <- matrix(vector, nrow=r, ncol=c,byrow=TRUE, dimnames =
list(rownames,colnames))

VRNath (JNTU KAKINADA) UG-STAT 16 / 34


Matrices

A matrix is a two-dimensional array


Each element should be of same mode (numeric, character, or
logical)
Matrices are created with the matrix() function.

General R Command
mymatrix <- matrix(vector, nrow=r, ncol=c,byrow=TRUE, dimnames =
list(rownames,colnames))

no of rows(r), no of columns(c), byrow(fills row wise), dimnames (to


give names to columns and rows)

VRNath (JNTU KAKINADA) UG-STAT 16 / 34


Creating a R Matrices

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4
> x[1,4]

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4
> x[1,4]
[1] 7

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4
> x[1,4]
[1] 7
> x[1, c(4,5)]

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4
> x[1,4]
[1] 7
> x[1, c(4,5)]
[1] 7 9

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Creating a R Matrices

Example
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
> x[2,]
[1] 2 4 6 8 10
> x[,2]
[1] 3 4
> x[1,4]
[1] 7
> x[1, c(4,5)]
[1] 7 9

VRNath (JNTU KAKINADA) UG-STAT 17 / 34


Arrays in R

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1,
dim2, dim3))

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1,
dim2, dim3))
> z

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1,
dim2, dim3))
> z
, , C1

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1,
dim2, dim3))
> z
, , C1
B1 B2 B3

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1,
dim2, dim3))
> z
, , C1
B1 B2 B3
A1 1 3 5

VRNath (JNTU KAKINADA) UG-STAT 18 / 34


Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1,
dim2, dim3))
> z
, , C1
B1 B2 B3
A1 1 3 5
A2 2 4 6
VRNath (JNTU KAKINADA) UG-STAT 18 / 34
Arrays in R
Similar to matrices but can have more than two dimensions.
R Command
myarray <- array(vector, dimensions, dimnames)

An Example of R Array
> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1,
dim2, dim3))
> z
, , C1
B1 B2 B3
A1 1 3 5
A2 2 4 6
VRNath (JNTU KAKINADA) UG-STAT 18 / 34
Data frames

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age
1 1 25

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age
1 1 25
2 2 34

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age
1 1 25
2 2 34
3 3 28

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age
1 1 25
2 2 34
3 3 28
4 4 52

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age
1 1 25
2 2 34
3 3 28
4 4 52

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age
1 1 25
2 2 34
3 3 28
4 4 52

> patientdata[c("diabetes", "status")] Prints dataframe by variable


names

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age
1 1 25
2 2 34
3 3 28
4 4 52

> patientdata[c("diabetes", "status")] Prints dataframe by variable


names
> patientdata$age

VRNath (JNTU KAKINADA) UG-STAT 19 / 34


Data frames
More general than a matrix in that different columns can contain
different modes of data (numeric, character, and so on). Similar to
dataset in SAS, SPSS, and Stata.
Typical Data frame
>patientdata[1:2]
patientID age
1 1 25
2 2 34
3 3 28
4 4 52

> patientdata[c("diabetes", "status")] Prints dataframe by variable


names
> patientdata$age
[1] 25 34 28 52
VRNath (JNTU KAKINADA) UG-STAT 19 / 34
Variables from Data frames

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found
>summary(mtcars$mpg) correct

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found
>summary(mtcars$mpg) correct
>attach(mtcars) correct

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found
>summary(mtcars$mpg) correct
>attach(mtcars) correct
>summary(mpg) correct

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found
>summary(mtcars$mpg) correct
>attach(mtcars) correct
>summary(mpg) correct
>detach(mtcars) correct

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found
>summary(mtcars$mpg) correct
>attach(mtcars) correct
>summary(mpg) correct
>detach(mtcars) correct
>summary(mpg) Error msg the object mpg not found

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found
>summary(mtcars$mpg) correct
>attach(mtcars) correct
>summary(mpg) correct
>detach(mtcars) correct
>summary(mpg) Error msg the object mpg not found

>with(mtcars, print(summary(mpg))) correct

VRNath (JNTU KAKINADA) UG-STAT 20 / 34


Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found
>summary(mtcars$mpg) correct
>attach(mtcars) correct
>summary(mpg) correct
>detach(mtcars) correct
>summary(mpg) Error msg the object mpg not found

>with(mtcars, print(summary(mpg))) correct


>mpg Error msg the object mpg not found
VRNath (JNTU KAKINADA) UG-STAT 20 / 34
Variables from Data frames

R functions
attach() , detach(), with() functions

Some Examples
>data(mtcars) A dataset mtcars (with variables mpg,
disp) read
>summary(mpg) Error msg the object mpg not found
>summary(mtcars$mpg) correct
>attach(mtcars) correct
>summary(mpg) correct
>detach(mtcars) correct
>summary(mpg) Error msg the object mpg not found

>with(mtcars, print(summary(mpg))) correct


>mpg Error msg the object mpg not found
VRNath (JNTU KAKINADA) UG-STAT 20 / 34
Factors in R
The R function factor()

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, order=TRUE)

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, order=TRUE)
> patientdata <- [Link](patientID, age, diabetes, status)

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, order=TRUE)
> patientdata <- [Link](patientID, age, diabetes, status)
> str(patientdata)

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, order=TRUE)
> patientdata <- [Link](patientID, age, diabetes, status)
> str(patientdata)
’[Link]’: 4 obs. of 4 variables:

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, order=TRUE)
> patientdata <- [Link](patientID, age, diabetes, status)
> str(patientdata)
’[Link]’: 4 obs. of 4 variables:
$ patientID: num 1 2 3 4

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, order=TRUE)
> patientdata <- [Link](patientID, age, diabetes, status)
> str(patientdata)
’[Link]’: 4 obs. of 4 variables:
$ patientID: num 1 2 3 4
$ age : num 25 34 28 52

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, order=TRUE)
> patientdata <- [Link](patientID, age, diabetes, status)
> str(patientdata)
’[Link]’: 4 obs. of 4 variables:
$ patientID: num 1 2 3 4
$ age : num 25 34 28 52
$ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 1

VRNath (JNTU KAKINADA) UG-STAT 21 / 34


Factors in R
The R function factor()

Categorical (nominal) and ordered categorical (ordinal) variables in R


are called factors.
> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, order=TRUE)
> patientdata <- [Link](patientID, age, diabetes, status)
> str(patientdata)
’[Link]’: 4 obs. of 4 variables:
$ patientID: num 1 2 3 4
$ age : num 25 34 28 52
$ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 1
$ status : [Link] w/ 3 levels "Excellent"<"Improved"<..: 3 2 1 3
VRNath (JNTU KAKINADA) UG-STAT 21 / 34
The R Lists

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

An example of List

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

An example of List
> g <- "My First List"

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

An example of List
> g <- "My First List"
> h <- c(25, 26, 18, 39)

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

An example of List
> g <- "My First List"
> h <- c(25, 26, 18, 39)
> j <- matrix(1:10, nrow=5)

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

An example of List
> g <- "My First List"
> h <- c(25, 26, 18, 39)
> j <- matrix(1:10, nrow=5)
> k <- c("one", "two", "three")

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

An example of List
> g <- "My First List"
> h <- c(25, 26, 18, 39)
> j <- matrix(1:10, nrow=5)
> k <- c("one", "two", "three")
> mylist <- list(title=g, ages=h, j, k)

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

An example of List
> g <- "My First List"
> h <- c(25, 26, 18, 39)
> j <- matrix(1:10, nrow=5)
> k <- c("one", "two", "three")
> mylist <- list(title=g, ages=h, j, k)
> mylist

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


The R Lists

A list is an ordered collection of objects (components)possibly


unrelated
The R Code list()
mylist <- list(name1=object1,name2=object2,..)
names are optional

An example of List
> g <- "My First List"
> h <- c(25, 26, 18, 39)
> j <- matrix(1:10, nrow=5)
> k <- c("one", "two", "three")
> mylist <- list(title=g, ages=h, j, k)
> mylist

VRNath (JNTU KAKINADA) UG-STAT 22 / 34


Data Input
Direct Keyboard Input

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))
mydata <- edit(mydata)

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))
mydata <- edit(mydata)

Embed in the source

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))
mydata <- edit(mydata)

Embed in the source


mydatatxt <- "

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))
mydata <- edit(mydata)

Embed in the source


mydatatxt <- "
age gender weight

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))
mydata <- edit(mydata)

Embed in the source


mydatatxt <- "
age gender weight
25 m 166

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))
mydata <- edit(mydata)

Embed in the source


mydatatxt <- "
age gender weight
25 m 166
30 f 115

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))
mydata <- edit(mydata)

Embed in the source


mydatatxt <- "
age gender weight
25 m 166
30 f 115
18 f 120

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


Data Input
Direct Keyboard Input

Built-in Text editor edit()


mydata <- [Link](age=numeric(0)
gender=character(0), weight=numeric(0))
mydata <- edit(mydata)

Embed in the source


mydatatxt <- "
age gender weight
25 m 166
30 f 115
18 f 120
"
mydata <- [Link](header=TRUE, text=mydatatxt)

VRNath (JNTU KAKINADA) UG-STAT 23 / 34


R data input
Importing from Files

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names



sep->spaces "", comma ",",tab delimited ""

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names



sep->spaces "", comma ",",tab delimited ""
[Link]->Names to be given to rows

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names



sep->spaces "", comma ",",tab delimited ""
[Link]->Names to be given to rows
[Link]->Names to be given to columns

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names



sep->spaces "", comma ",",tab delimited ""
[Link]->Names to be given to rows
[Link]->Names to be given to columns
[Link]=c("-9", "?") converts each -9 and ?
value to NA

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names



sep->spaces "", comma ",",tab delimited ""
[Link]->Names to be given to rows
[Link]->Names to be given to columns
[Link]=c("-9", "?") converts each -9 and ?
value to NA
colClasses=c("numeric", "numeric", "character",
"NULL", "numeric")

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names



sep->spaces "", comma ",",tab delimited ""
[Link]->Names to be given to rows
[Link]->Names to be given to columns
[Link]=c("-9", "?") converts each -9 and ?
value to NA
colClasses=c("numeric", "numeric", "character",
"NULL", "numeric")
skip=Number of lines to skipped from the top

VRNath (JNTU KAKINADA) UG-STAT 24 / 34


R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names



sep->spaces "", comma ",",tab delimited ""
[Link]->Names to be given to rows
[Link]->Names to be given to columns
[Link]=c("-9", "?") converts each -9 and ?
value to NA
colClasses=c("numeric", "numeric", "character",
"NULL", "numeric")
skip=Number of lines to skipped from the top
stringsAsFactors=Whether to convert the character
vectors to factors
VRNath (JNTU KAKINADA) UG-STAT 24 / 34
R data input
Importing from Files

Using [Link]()
mydataframe <- [Link](file, options)

header->Whether top row contains var names



sep->spaces "", comma ",",tab delimited ""
[Link]->Names to be given to rows
[Link]->Names to be given to columns
[Link]=c("-9", "?") converts each -9 and ?
value to NA
colClasses=c("numeric", "numeric", "character",
"NULL", "numeric")
skip=Number of lines to skipped from the top
stringsAsFactors=Whether to convert the character
vectors to factors
VRNath (JNTU KAKINADA) UG-STAT 24 / 34
Data Management

VRNath (JNTU KAKINADA) UG-STAT 25 / 34


Data Management

Combining Data frames

VRNath (JNTU KAKINADA) UG-STAT 25 / 34


Data Management

Combining Data frames


class(object) Gives the class of an object.

VRNath (JNTU KAKINADA) UG-STAT 25 / 34


Data Management

Combining Data frames


class(object) Gives the class of an object.
mode(object) Determines how an object is stored.

VRNath (JNTU KAKINADA) UG-STAT 25 / 34


Data Management

Combining Data frames


class(object) Gives the class of an object.
mode(object) Determines how an object is stored.
names(object) Gives the names of components in an
object.

VRNath (JNTU KAKINADA) UG-STAT 25 / 34


Data Management

Combining Data frames


class(object) Gives the class of an object.
mode(object) Determines how an object is stored.
names(object) Gives the names of components in an
object.
c(object, object,...) Combines objects into a
vector.

VRNath (JNTU KAKINADA) UG-STAT 25 / 34


Data Management

Combining Data frames


class(object) Gives the class of an object.
mode(object) Determines how an object is stored.
names(object) Gives the names of components in an
object.
c(object, object,...) Combines objects into a
vector.
cbind(object, object, ...) Combines objects as
columns.

VRNath (JNTU KAKINADA) UG-STAT 25 / 34


Data Management

Combining Data frames


class(object) Gives the class of an object.
mode(object) Determines how an object is stored.
names(object) Gives the names of components in an
object.
c(object, object,...) Combines objects into a
vector.
cbind(object, object, ...) Combines objects as
columns.
rbind(object, object, ...) Combines objects as
rows.

VRNath (JNTU KAKINADA) UG-STAT 25 / 34


Viewing Data

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Viewing Data

Some R functions

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Viewing Data

Some R functions
head(object) Lists the first part of an object.

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Viewing Data

Some R functions
head(object) Lists the first part of an object.
tail(object) Lists the last part of an object.

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Viewing Data

Some R functions
head(object) Lists the first part of an object.
tail(object) Lists the last part of an object.
ls() Lists current objects.

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Viewing Data

Some R functions
head(object) Lists the first part of an object.
tail(object) Lists the last part of an object.
ls() Lists current objects.
rm(object, object, ...) Deletes one or more
objects.

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Viewing Data

Some R functions
head(object) Lists the first part of an object.
tail(object) Lists the last part of an object.
ls() Lists current objects.
rm(object, object, ...) Deletes one or more
objects.
The statement rm(list =ls()) removes most objects.

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Viewing Data

Some R functions
head(object) Lists the first part of an object.
tail(object) Lists the last part of an object.
ls() Lists current objects.
rm(object, object, ...) Deletes one or more
objects.
The statement rm(list =ls()) removes most objects.
newobject <- edit(object) Edits object and saves it
as newobject.

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Viewing Data

Some R functions
head(object) Lists the first part of an object.
tail(object) Lists the last part of an object.
ls() Lists current objects.
rm(object, object, ...) Deletes one or more
objects.
The statement rm(list =ls()) removes most objects.
newobject <- edit(object) Edits object and saves it
as newobject.
fix(object) Edits an object in place.

VRNath (JNTU KAKINADA) UG-STAT 26 / 34


Creating New Variables

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

Using attach()

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

Using attach()
attach(mydata)

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

Using attach()
attach(mydata)
mydata$sumx <- x1 + x2

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

Using attach()
attach(mydata)
mydata$sumx <- x1 + x2
mydata$meanx <- (x1 + x2)/2

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

Using attach()
attach(mydata)
mydata$sumx <- x1 + x2
mydata$meanx <- (x1 + x2)/2
detach(mydata)

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

Using attach()
attach(mydata)
mydata$sumx <- x1 + x2
mydata$meanx <- (x1 + x2)/2
detach(mydata)

Using transform()

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Creating New Variables
mydata<-[Link](x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
Using $
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2

Using attach()
attach(mydata)
mydata$sumx <- x1 + x2
mydata$meanx <- (x1 + x2)/2
detach(mydata)

Using transform()
mydata <- transform(mydata,sumx = x1 + x2,meanx = (x1 + x2)/2)

VRNath (JNTU KAKINADA) UG-STAT 27 / 34


Recoding Variables

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Recoding Variables

Recoding involves creating new values of a variable conditional on the


existing values

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Recoding Variables

Recoding involves creating new values of a variable conditional on the


existing values
The R function

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Recoding Variables

Recoding involves creating new values of a variable conditional on the


existing values
The R function
variable[condition] <- expression

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Recoding Variables

Recoding involves creating new values of a variable conditional on the


existing values
The R function
variable[condition] <- expression
leadership$agecat[leadership$age > 75] <- "Elder"

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Recoding Variables

Recoding involves creating new values of a variable conditional on the


existing values
The R function
variable[condition] <- expression
leadership$agecat[leadership$age > 75] <- "Elder"
leadership$agecat[leadership$age >= 55 &

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Recoding Variables

Recoding involves creating new values of a variable conditional on the


existing values
The R function
variable[condition] <- expression
leadership$agecat[leadership$age > 75] <- "Elder"
leadership$agecat[leadership$age >= 55 &
leadership$age <= 75] <- "Middle Aged"

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Recoding Variables

Recoding involves creating new values of a variable conditional on the


existing values
The R function
variable[condition] <- expression
leadership$agecat[leadership$age > 75] <- "Elder"
leadership$agecat[leadership$age >= 55 &
leadership$age <= 75] <- "Middle Aged"
leadership$agecat[leadership$age < 55] <- "Young"

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Recoding Variables

Recoding involves creating new values of a variable conditional on the


existing values
The R function
variable[condition] <- expression
leadership$agecat[leadership$age > 75] <- "Elder"
leadership$agecat[leadership$age >= 55 &
leadership$age <= 75] <- "Middle Aged"
leadership$agecat[leadership$age < 55] <- "Young"

leadership <- within(leadership, agecat <- NA agecat[age > 75] <-


"Elder" agecat[age >= 55 & age <= 75] <- "Middle Aged" agecat[age <
55] <- "Young" )

VRNath (JNTU KAKINADA) UG-STAT 28 / 34


Renaming the Variables

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively
fix(mydata)

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively
fix(mydata)
Click on the variable name and change it

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively
fix(mydata)
Click on the variable name and change it

Programmatically

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively
fix(mydata)
Click on the variable name and change it

Programmatically
>names(mydata)

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively
fix(mydata)
Click on the variable name and change it

Programmatically
>names(mydata)
If we want to change the third variable

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively
fix(mydata)
Click on the variable name and change it

Programmatically
>names(mydata)
If we want to change the third variable >names(mydata)[3]<-newVar

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively
fix(mydata)
Click on the variable name and change it

Programmatically
>names(mydata)
If we want to change the third variable >names(mydata)[3]<-newVar

names(leadership)[6:10] <- c("item1", "item2", "item3", "item4",


"item5")

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Renaming the Variables

We can change the variable names either interactively or


programatically
Interactively
fix(mydata)
Click on the variable name and change it

Programmatically
>names(mydata)
If we want to change the third variable >names(mydata)[3]<-newVar

names(leadership)[6:10] <- c("item1", "item2", "item3", "item4",


"item5")
We can use paste() function to generates names vector

VRNath (JNTU KAKINADA) UG-STAT 29 / 34


Missing or Faulty values

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA
2 [Link]() tests whether the object has missing values

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA
2 [Link]() tests whether the object has missing values
3 Recode the missing value as NA

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA
2 [Link]() tests whether the object has missing values
3 Recode the missing value as NA
leadership$age[leadership$age == 99] <- NA

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA
2 [Link]() tests whether the object has missing values
3 Recode the missing value as NA
leadership$age[leadership$age == 99] <- NA
4 Exclude the missing values in options [Link]=TRUE

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA
2 [Link]() tests whether the object has missing values
3 Recode the missing value as NA
leadership$age[leadership$age == 99] <- NA
4 Exclude the missing values in options [Link]=TRUE
5 Exclude the records that contain missing values

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA
2 [Link]() tests whether the object has missing values
3 Recode the missing value as NA
leadership$age[leadership$age == 99] <- NA
4 Exclude the missing values in options [Link]=TRUE
5 Exclude the records that contain missing values
newdata <- [Link](leadership)

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA
2 [Link]() tests whether the object has missing values
3 Recode the missing value as NA
leadership$age[leadership$age == 99] <- NA
4 Exclude the missing values in options [Link]=TRUE
5 Exclude the records that contain missing values
newdata <- [Link](leadership)

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


Missing or Faulty values

Steps to taken
1 Missing values are represented by NA
2 [Link]() tests whether the object has missing values
3 Recode the missing value as NA
leadership$age[leadership$age == 99] <- NA
4 Exclude the missing values in options [Link]=TRUE
5 Exclude the records that contain missing values
newdata <- [Link](leadership)

Sometimes NA values will be replaced by the average of the available


values. In case of categorical variables they are replaced by mode.

VRNath (JNTU KAKINADA) UG-STAT 30 / 34


R Lab Exercise-1

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 5 5 5

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 5 5 5
2 2 10/28/08 US F 40 3 5 2 5 5

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 5 5 5
2 2 10/28/08 US F 40 3 5 2 5 5
3 3 10/01/08 UK F 25 3 5 5 5 2

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 5 5 5
2 2 10/28/08 US F 40 3 5 2 5 5
3 3 10/01/08 UK F 25 3 5 5 5 2
4 4 10/12/08 UK M 39 3 3 4 NA NA

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 5 5 5
2 2 10/28/08 US F 40 3 5 2 5 5
3 3 10/01/08 UK F 25 3 5 5 5 2
4 4 10/12/08 UK M 39 3 3 4 NA NA
5 5 05/01/09 UK F NA 2 2 1 2 1"

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 5 5 5
2 2 10/28/08 US F 40 3 5 2 5 5
3 3 10/01/08 UK F 25 3 5 5 5 2
4 4 10/12/08 UK M 39 3 3 4 NA NA
5 5 05/01/09 UK F NA 2 2 1 2 1"
leadership<-[Link](header=TRUE, text =
leadershiptxt)

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 5 5 5
2 2 10/28/08 US F 40 3 5 2 5 5
3 3 10/01/08 UK F 25 3 5 5 5 2
4 4 10/12/08 UK M 39 3 3 4 NA NA
5 5 05/01/09 UK F NA 2 2 1 2 1"
leadership<-[Link](header=TRUE, text =
leadershiptxt)
newdata <- [Link](leadership)

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-1

Observe The Output


leadershiptxt<-"
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 5 5 5
2 2 10/28/08 US F 40 3 5 2 5 5
3 3 10/01/08 UK F 25 3 5 5 5 2
4 4 10/12/08 UK M 39 3 3 4 NA NA
5 5 05/01/09 UK F NA 2 2 1 2 1"
leadership<-[Link](header=TRUE, text =
leadershiptxt)
newdata <- [Link](leadership)
newdata

VRNath (JNTU KAKINADA) UG-STAT 31 / 34


R Lab Exercise-2

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)
newdata <- leadership[order(gender, age),]

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)
newdata <- leadership[order(gender, age),]
detach(leadership)

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)
newdata <- leadership[order(gender, age),]
detach(leadership)
sorts the rows into female followed by male, and
youngest to oldest within each gender.

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)
newdata <- leadership[order(gender, age),]
detach(leadership)
sorts the rows into female followed by male, and
youngest to oldest within each gender.
Finally,

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)
newdata <- leadership[order(gender, age),]
detach(leadership)
sorts the rows into female followed by male, and
youngest to oldest within each gender.
Finally,
attach(leadership)

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)
newdata <- leadership[order(gender, age),]
detach(leadership)
sorts the rows into female followed by male, and
youngest to oldest within each gender.
Finally,
attach(leadership)
newdata <-leadership[order(gender, -age),]

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)
newdata <- leadership[order(gender, age),]
detach(leadership)
sorts the rows into female followed by male, and
youngest to oldest within each gender.
Finally,
attach(leadership)
newdata <-leadership[order(gender, -age),]
detach(leadership)

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


R Lab Exercise-2

Observe The Output


attach(leadership)
newdata <- leadership[order(gender, age),]
detach(leadership)
sorts the rows into female followed by male, and
youngest to oldest within each gender.
Finally,
attach(leadership)
newdata <-leadership[order(gender, -age),]
detach(leadership)
sorts the rows by gender

VRNath (JNTU KAKINADA) UG-STAT 32 / 34


Merging datasets

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.


merge()
total <- merge(dataframeA, dataframeB, by="ID")

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.


merge()
total <- merge(dataframeA, dataframeB, by="ID")
total <- merge(dataframeA, dataframeB, by=c("ID","Country"))

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.


merge()
total <- merge(dataframeA, dataframeB, by="ID")
total <- merge(dataframeA, dataframeB, by=c("ID","Country"))

rbind()

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.


merge()
total <- merge(dataframeA, dataframeB, by="ID")
total <- merge(dataframeA, dataframeB, by=c("ID","Country"))

rbind()
To join two data frames (datasets) vertically

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.


merge()
total <- merge(dataframeA, dataframeB, by="ID")
total <- merge(dataframeA, dataframeB, by=c("ID","Country"))

rbind()
To join two data frames (datasets) vertically
total <- rbind(dataframeA, dataframeB)

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.


merge()
total <- merge(dataframeA, dataframeB, by="ID")
total <- merge(dataframeA, dataframeB, by=c("ID","Country"))

rbind()
To join two data frames (datasets) vertically
total <- rbind(dataframeA, dataframeB)

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.


merge()
total <- merge(dataframeA, dataframeB, by="ID")
total <- merge(dataframeA, dataframeB, by=c("ID","Country"))

rbind()
To join two data frames (datasets) vertically
total <- rbind(dataframeA, dataframeB)

cbind()
To join two data frames horizontally

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Merging datasets

To add columns (variables) and rows (observations) to a data frame.


merge()
total <- merge(dataframeA, dataframeB, by="ID")
total <- merge(dataframeA, dataframeB, by=c("ID","Country"))

rbind()
To join two data frames (datasets) vertically
total <- rbind(dataframeA, dataframeB)

cbind()
To join two data frames horizontally
total <- cbind(A, B)

VRNath (JNTU KAKINADA) UG-STAT 33 / 34


Subsetting datasets
Selecting Variables

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set
newdata <- leadership[, c(6:10)]

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set
newdata <- leadership[, c(6:10)]
Selects col 1 to col 5. Leaving the row blank selects all rows

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set
newdata <- leadership[, c(6:10)]
Selects col 1 to col 5. Leaving the row blank selects all rows

Selecting Variables from Large Set

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set
newdata <- leadership[, c(6:10)]
Selects col 1 to col 5. Leaving the row blank selects all rows

Selecting Variables from Large Set


myvars <- c("q1", "q2", "q3", "q4", "q5")

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set
newdata <- leadership[, c(6:10)]
Selects col 1 to col 5. Leaving the row blank selects all rows

Selecting Variables from Large Set


myvars <- c("q1", "q2", "q3", "q4", "q5")
newdata <-leadership[myvars]

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set
newdata <- leadership[, c(6:10)]
Selects col 1 to col 5. Leaving the row blank selects all rows

Selecting Variables from Large Set


myvars <- c("q1", "q2", "q3", "q4", "q5")
newdata <-leadership[myvars]

paste() function

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set
newdata <- leadership[, c(6:10)]
Selects col 1 to col 5. Leaving the row blank selects all rows

Selecting Variables from Large Set


myvars <- c("q1", "q2", "q3", "q4", "q5")
newdata <-leadership[myvars]

paste() function
myvars <- paste("q", 1:5, sep="")

VRNath (JNTU KAKINADA) UG-STAT 34 / 34


Subsetting datasets
Selecting Variables

To select and exclude variables, observations or both.


Excluding Variables from Large Set
newdata <- leadership[, c(6:10)]
Selects col 1 to col 5. Leaving the row blank selects all rows

Selecting Variables from Large Set


myvars <- c("q1", "q2", "q3", "q4", "q5")
newdata <-leadership[myvars]

paste() function
myvars <- paste("q", 1:5, sep="")
newdata <- leadership[myvars]

VRNath (JNTU KAKINADA) UG-STAT 34 / 34

You might also like