SlideShare a Scribd company logo
1 of 19
INTRODUCTION
TO R
Content
1:Introduction to R
2:Functions and workspace
3:Data Cleaning and tidying in R
4:Visualizing Data part 1
5:Visualizing Data part 2
Lab Outcomes
1. What is R
1. Invoke the R environment and examine the
R workspace
1. R Basics
What is R
▪R is a free open source package
based on the S language developed
by Bell Labs
▪Statistical Programming Language
used to develop statistical software
▪Used by statisticians and data miners
▪Many statistical functions are already
built in
Why R
1. Implement statistical
procedures
2. Provide excellent graphics
functionality
3. Excellent start for data analysis
projects
Getting Started
◦Where to get R?
◦Go to www.r-project.org
◦Downloads: CRAN
◦Set your Mirror: Anyone in the USA is fine.
◦Select Windows 95 or later.
◦ Select base.
◦ Select R-3.4.3 for Windows
◦ The others are if you are a developer and
wish to change the source code.
R Basics
▪Objects
▪Naming Convention
▪Assignment
▪Built-In Functions
▪Example Objects:
Vectors, Lists, Data Frames
▪Control Statements
▪Functions
▪Workspace
Objects
◦ Objects should have names
◦ Object Types: vector, matrix … etc.
◦ Object Attributes
◦ mode: numeric, character, boolean
◦ length: number of elements in object
◦ Object Values
◦ assign a value
◦ create a blank object
Naming Convention
◦ must start with a letter (A-Z or a-z)
◦ can contain letters, digits (0-9), and/or periods “.”
◦ ex: Var1.1
◦ case-sensitive
◦ mydata different from MyData
◦ do not use underscore “_”
Assignment
“<-” used to indicate assignment
▪ > x<-1
▪ > y<-3
▪ > z<-4
▪ > x*y*z
[1] 12
Note: Type determined automatically when variable is
created with "<-" operator
Built-In Functions
▪actions can be performed on objects
using functions
▪have arguments and options
▪provide a result
▪parentheses () are used to specify that
a function is being called
Example for Functions
> rep (1,10)
[1] 1 1 1 1 1 1 1 1 1 1
> seq (2,6)
[1] 2 3 4 5 6
> seq (4,20, by=4)
[1] 4 8 12 16 20
> x <- c (2,0,0,4)
> x * 4
[1] 8 0 016
>sqrt(x)
[1] 1.41 0.00 0.00 2.00
[Mean , Median, Variance, SD]
⮚x <- c(1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6) # our data set
⮚> mean.result = mean(x) # calculate mean
⮚ > print (mean.result)
⮚[1] 2.8
⮚x <- c(1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6) # our data set
⮚> median.result = median(x) # calculate median
⮚> print (median.result)
⮚[1] 2.5
⮚ variance.result = var(x) # calculate variance
⮚ > print (variance.result)
⮚ [1] 2.484211
⮚sd.result = sqrt(var(x)) # calculate standard deviation
⮚> print (sd.result)
⮚[1] 1.576138
Objects | Vectors
◦ A series of numbers
◦ Created with:
◦ c() to concatenate elements or sub-vectors
◦ rep() to repeat elements or patterns
◦ seq() or m:n to generate sequences
◦ Example:
◦ X <- c(2,0,0,9)
◦ Y <- seq(2,5) #sequence of integers between 2 & 5
◦ Z <- rep(1,4) #repeat the number 1, 4 times
◦ X+Y+Z
◦ ? *
Objects | Accessing Vectors
> x <- c (2,0,0,4)
> x [1] # Select the first element, equivalent to x[c(1)]
[1] 2
x [-1] # Exclude the first element
[1] 0 0 4
> x [1] <- 3 ; x
[1] 3 0 0 4
> x [-1] = 5 ; x
[1] 3 5 5 5
>x<5
[1] TRUE FALSE FALSE FALSE
> x [x<5] = 2 #Edits elements meeting condition
[1] 2 5 5 5
Objects | Data Frames
▪ A group or collection of Vectors
▪ Most of the time, when data is loaded, it will be organized in a data frame
Example:
>DF <- data.frame (h=c(150,160), w=c(65,72))
>DF
h w
1 150 65 df[-1,2]
2 160 72
Objects | Accessing Data
Frames
> DF[1]
h
1 150
2 160
> DF[1,]
h w
150 65
> DF[2]
w
1 65
2 72
> DF[2,]
h w
160 72
Q1: DF[1,2] ?
Q2:DF[-1,2] ?
Hands ON
◦ 1-Create A data Frame with 3 columns :
◦ 1st col = 1,2,3
◦ 2nd col = 10,14,10
◦ 3rd col = 0,0,0
◦ 2-Multiply 1st & 2nd columns and put the result in the 3rd
columns then print the data frame in the same line.
THANK YOU ☺

More Related Content

Similar to BA lab1.pptx

ComputeFest 2012: Intro To R for Physical Sciences
ComputeFest 2012: Intro To R for Physical SciencesComputeFest 2012: Intro To R for Physical Sciences
ComputeFest 2012: Intro To R for Physical Sciencesalexstorer
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RKevin Smith
 
Python bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of NairobiPython bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of Nairobikrmboya
 
Functional Operations - Susan Potter
Functional Operations - Susan PotterFunctional Operations - Susan Potter
Functional Operations - Susan Potterdistributed matters
 
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"LogeekNightUkraine
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSonaCharles2
 
A gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojureA gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojurePaul Lam
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 

Similar to BA lab1.pptx (20)

ComputeFest 2012: Intro To R for Physical Sciences
ComputeFest 2012: Intro To R for Physical SciencesComputeFest 2012: Intro To R for Physical Sciences
ComputeFest 2012: Intro To R for Physical Sciences
 
R and data mining
R and data miningR and data mining
R and data mining
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into R
 
Python bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of NairobiPython bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of Nairobi
 
Functional Operations - Susan Potter
Functional Operations - Susan PotterFunctional Operations - Susan Potter
Functional Operations - Susan Potter
 
Tutorialmatlab kurniawan.s
Tutorialmatlab kurniawan.sTutorialmatlab kurniawan.s
Tutorialmatlab kurniawan.s
 
Tutorial matlab
Tutorial matlabTutorial matlab
Tutorial matlab
 
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop  - Xi...
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
 
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"
 
Flink internals web
Flink internals web Flink internals web
Flink internals web
 
Machine Learning in R
Machine Learning in RMachine Learning in R
Machine Learning in R
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MD
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Big datacourse
Big datacourseBig datacourse
Big datacourse
 
A gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojureA gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojure
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 

Recently uploaded

Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...Boston Institute of Analytics
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degreeyuu sss
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfBoston Institute of Analytics
 

Recently uploaded (20)

Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
 

BA lab1.pptx

  • 2. Content 1:Introduction to R 2:Functions and workspace 3:Data Cleaning and tidying in R 4:Visualizing Data part 1 5:Visualizing Data part 2
  • 3. Lab Outcomes 1. What is R 1. Invoke the R environment and examine the R workspace 1. R Basics
  • 4. What is R ▪R is a free open source package based on the S language developed by Bell Labs ▪Statistical Programming Language used to develop statistical software ▪Used by statisticians and data miners ▪Many statistical functions are already built in
  • 5. Why R 1. Implement statistical procedures 2. Provide excellent graphics functionality 3. Excellent start for data analysis projects
  • 6. Getting Started ◦Where to get R? ◦Go to www.r-project.org ◦Downloads: CRAN ◦Set your Mirror: Anyone in the USA is fine. ◦Select Windows 95 or later. ◦ Select base. ◦ Select R-3.4.3 for Windows ◦ The others are if you are a developer and wish to change the source code.
  • 7. R Basics ▪Objects ▪Naming Convention ▪Assignment ▪Built-In Functions ▪Example Objects: Vectors, Lists, Data Frames ▪Control Statements ▪Functions ▪Workspace
  • 8. Objects ◦ Objects should have names ◦ Object Types: vector, matrix … etc. ◦ Object Attributes ◦ mode: numeric, character, boolean ◦ length: number of elements in object ◦ Object Values ◦ assign a value ◦ create a blank object
  • 9. Naming Convention ◦ must start with a letter (A-Z or a-z) ◦ can contain letters, digits (0-9), and/or periods “.” ◦ ex: Var1.1 ◦ case-sensitive ◦ mydata different from MyData ◦ do not use underscore “_”
  • 10. Assignment “<-” used to indicate assignment ▪ > x<-1 ▪ > y<-3 ▪ > z<-4 ▪ > x*y*z [1] 12 Note: Type determined automatically when variable is created with "<-" operator
  • 11. Built-In Functions ▪actions can be performed on objects using functions ▪have arguments and options ▪provide a result ▪parentheses () are used to specify that a function is being called
  • 12. Example for Functions > rep (1,10) [1] 1 1 1 1 1 1 1 1 1 1 > seq (2,6) [1] 2 3 4 5 6 > seq (4,20, by=4) [1] 4 8 12 16 20 > x <- c (2,0,0,4) > x * 4 [1] 8 0 016 >sqrt(x) [1] 1.41 0.00 0.00 2.00
  • 13. [Mean , Median, Variance, SD] ⮚x <- c(1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6) # our data set ⮚> mean.result = mean(x) # calculate mean ⮚ > print (mean.result) ⮚[1] 2.8 ⮚x <- c(1,2,3,4,5,1,2,3,1,2,4,5,2,3,1,1,2,3,5,6) # our data set ⮚> median.result = median(x) # calculate median ⮚> print (median.result) ⮚[1] 2.5 ⮚ variance.result = var(x) # calculate variance ⮚ > print (variance.result) ⮚ [1] 2.484211 ⮚sd.result = sqrt(var(x)) # calculate standard deviation ⮚> print (sd.result) ⮚[1] 1.576138
  • 14. Objects | Vectors ◦ A series of numbers ◦ Created with: ◦ c() to concatenate elements or sub-vectors ◦ rep() to repeat elements or patterns ◦ seq() or m:n to generate sequences ◦ Example: ◦ X <- c(2,0,0,9) ◦ Y <- seq(2,5) #sequence of integers between 2 & 5 ◦ Z <- rep(1,4) #repeat the number 1, 4 times ◦ X+Y+Z ◦ ? *
  • 15. Objects | Accessing Vectors > x <- c (2,0,0,4) > x [1] # Select the first element, equivalent to x[c(1)] [1] 2 x [-1] # Exclude the first element [1] 0 0 4 > x [1] <- 3 ; x [1] 3 0 0 4 > x [-1] = 5 ; x [1] 3 5 5 5 >x<5 [1] TRUE FALSE FALSE FALSE > x [x<5] = 2 #Edits elements meeting condition [1] 2 5 5 5
  • 16. Objects | Data Frames ▪ A group or collection of Vectors ▪ Most of the time, when data is loaded, it will be organized in a data frame Example: >DF <- data.frame (h=c(150,160), w=c(65,72)) >DF h w 1 150 65 df[-1,2] 2 160 72
  • 17. Objects | Accessing Data Frames > DF[1] h 1 150 2 160 > DF[1,] h w 150 65 > DF[2] w 1 65 2 72 > DF[2,] h w 160 72 Q1: DF[1,2] ? Q2:DF[-1,2] ?
  • 18. Hands ON ◦ 1-Create A data Frame with 3 columns : ◦ 1st col = 1,2,3 ◦ 2nd col = 10,14,10 ◦ 3rd col = 0,0,0 ◦ 2-Multiply 1st & 2nd columns and put the result in the 3rd columns then print the data frame in the same line.