SlideShare a Scribd company logo
R Programming – Introduction and
Vectors
Ivo Bernardo – Udemy Course
The R Language
“R is a language and environment for statistical computing and graphics
[…] R provides a wide variety of statistical (linear and nonlinear modelling,
classical statistical tests, time-series analysis, classification, clustering, …)
and graphical techniques, and is highly extensible.”
R Usage
- Use as a calculator.
- Compute several statistics about data.
- Plot data.
- Develop machine learning algorithms.
Complexity
R as a Calculator
R performs most of the mathematical calculations you can think of:
- 1 + 2 on the console yields 3
- 100-50,6 yields 49.4
- ...
R has special functions to compute well known mathematical operations:
- sqrt() function computes a square-root.
- exp() computes an exponential.
- log() computes the logarithm.
R as a Calculator
You can also perform complex calculations that obey to mathematical
rules:
- (10/10) / (5/5) = 1
- exp(0) * 20 = 20
- exp(sqrt(9)) = 20.08554
R objects
Most of the stuff we use in R are objects.
These objects have their:
- Own set of characteristics (size, dimensions, data types, etc.)
- Own set of functions.
Example of R objects:
- Vector;
- Matrix;
- List;
- DataFrame;
R Vectors
One of the most simple, yet powerful, object is the vector.
- Vectors are characterized by their size and type of the elements.
- All elements have to be of the same type in a vector.
- Vectors are index-based and you can access the elements by their
position (index).
- You create a vector with the command c()
- To create a vector melons with 4 melons and their weight, in kilograms:
- melons <- c(3.4, 3.1, 3, 4.5)
- To access the weight of melon 1 we would use melons[1]
The Environment
Assigning objects into variables using = or <- is the same!
melons = c(3.4, 3.1, 3, 4.5)
melons <- c(3.4, 3.1, 3, 4.5)
Vector Operations
It would be good if R would let us make calculations on our vector..
melons <- c(3.4, 3.1, 3, 4.5)
“There was a problem when weighting, the melons have only half the
weight”
melons/2
Each item in the vector is divided by 2!
“There was a problem when weighting, the melons have two times
the weight”
bananas*2
Each item in the vector is multiplied by 2!
Vector Operations
melons <- c(3.4, 3.1, 3, 4.5)
“There was a problem on the measurements, you have to add these
values to their weight: 0.4, 0.2, 0.4, 0.3”
Melons + c(0.4, 0.2, 0.4, 0.3)
Each melon will have it’s weight summed with the corresponding
element of the new vector so the resulting vector will be:
(3.4+0.4, 3,1+0.2, 3+0.4, 4+0.3)
The cool thing is that we can make this more meaningful by calling
the c(0.4, 0.2, 0.4, 0.3) vector something related such as
adjust_weight and then our calculation could be:
new_melons = melons+adjust_weight
Vector Operations
melons <- c(3.4, 3.1, 3, 4.5)
“The value of the melons weight is the square root of the value we
gave you”
sqrt(melons)
“Sum all the melons’ weight please”
sum(melons) would yield 14
“What’s the mean of the melons’ length?”
mean(melons) would yield 3.5
Vector Operations
melons <- c(3.4, 3.1, 3, 4.5)
“The value of the melons weight is the square root of the value we
gave you”
sqrt(melons)
“Sum all the melons’ weight please”
sum(melons) would yield 14
“What’s the mean of the melons’ weight?”
mean(melons) would yield 3.5
Vector Operations
melons <- c(3.4, 3.1, 3, 4.5)
R also let you do comparison calculations:
“Which Melons weight more than 4 kilograms?”
melons>4 This would yield:
[FALSE, FALSE, FALSE, TRUE]
“Which Melons weight exactly 3 kilograms?”
melons==3 This would yield:
[FALSE, FALSE, TRUE, FALSE]
Vector Operations
melons <- c(3.4, 3.1, 3, 4.5)
AND and OR are also possible:
“Which Melons weight exacly 4 kilograms and 3 kilograms?”
melons == 4 & melons== 3 This would yield:
[FALSE, FALSE, FALSE, FALSE]
“Which Melons weight exacly 4 kilograms or 3 kilograms?”
melons == 4 | melons== 3 This would yield:
[FALSE, FALSE, TRUE, FALSE]
Watch out!
When you divide something by 0 you get the symbol meaning
infinite:
Inf
If you use a vector that has an Infinite element in it, our calculations
do not stand:
sum(c(3.4, 3.1, 3, 4.5/0)) would yield Inf
The same happens for NaN (short for Not a Number):
sum(c(3.4, 3.1, 3, sqrt(-1)) would yield NaN
The same happens for NA (short for Not Available):
sum(c(3.4, 3.1, 3, NA)) would yield NA
Watch out!
- NA’s are pretty common in data analysis so watch out for them.
Fortunately, most functions can dodge this by using an extra
argument na.rm = TRUE
sum(c(3.4, 3.1, 3, NA), na.rm=TRUE) would yield 9.5
mean(c(3.4, 3.1, 3, NA), na.rm=TRUE) would yield
3.166667
Want to discover more?
Join my R courses on Udemy risk-free (30 day refund policy), where you will
have the chance to learn with practical exercises:
- R for Absolute Beginners
- R for Data Science
Looking forward to see you there!

More Related Content

Similar to R Introduction - R Vectors and the Environment

introduction-to-functions-grade-11general-math.ppt
introduction-to-functions-grade-11general-math.pptintroduction-to-functions-grade-11general-math.ppt
introduction-to-functions-grade-11general-math.ppt
Pauline Misty Panganiban
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
Amba Research
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
Josh Doyle
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
Siva Arunachalam
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
vpletap
 
Curvefitting
CurvefittingCurvefitting
Curvefitting
Philberto Saroni
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
James Brotsos
 
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
Yo Halb
 
LectureSlides3.pdf
LectureSlides3.pdfLectureSlides3.pdf
LectureSlides3.pdf
ssuser7de1db1
 
Python.pptx
Python.pptxPython.pptx
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
062MayankSinghal
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir
 
Python introduction
Python introductionPython introduction
Python introduction
leela rani
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
Mohammed El Rafie Tarabay
 
Mit6 094 iap10_lec03
Mit6 094 iap10_lec03Mit6 094 iap10_lec03
Mit6 094 iap10_lec03
Tribhuwan Pant
 
R tutorial (R program 101)
R tutorial (R program 101)R tutorial (R program 101)
R tutorial (R program 101)
Gregory Choi, MBA, CISSP
 
R part I
R part IR part I
R part I
Ruru Chowdhury
 
Application of subQuan to Algebra: 3rd-8th grade and beyond...
Application of subQuan to Algebra: 3rd-8th grade and beyond...Application of subQuan to Algebra: 3rd-8th grade and beyond...
Application of subQuan to Algebra: 3rd-8th grade and beyond...
Dream Realizations
 
R workshop
R workshopR workshop
R workshop
Revanth19921
 

Similar to R Introduction - R Vectors and the Environment (20)

introduction-to-functions-grade-11general-math.ppt
introduction-to-functions-grade-11general-math.pptintroduction-to-functions-grade-11general-math.ppt
introduction-to-functions-grade-11general-math.ppt
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Curvefitting
CurvefittingCurvefitting
Curvefitting
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
 
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2
 
LectureSlides3.pdf
LectureSlides3.pdfLectureSlides3.pdf
LectureSlides3.pdf
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Python introduction
Python introductionPython introduction
Python introduction
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
 
Mit6 094 iap10_lec03
Mit6 094 iap10_lec03Mit6 094 iap10_lec03
Mit6 094 iap10_lec03
 
R tutorial (R program 101)
R tutorial (R program 101)R tutorial (R program 101)
R tutorial (R program 101)
 
R part I
R part IR part I
R part I
 
Application of subQuan to Algebra: 3rd-8th grade and beyond...
Application of subQuan to Algebra: 3rd-8th grade and beyond...Application of subQuan to Algebra: 3rd-8th grade and beyond...
Application of subQuan to Algebra: 3rd-8th grade and beyond...
 
R workshop
R workshopR workshop
R workshop
 

Recently uploaded

University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
soxrziqu
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
SaffaIbrahim1
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Kiwi Creative
 
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
xclpvhuk
 
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
wyddcwye1
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
Bill641377
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
ihavuls
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
AndrzejJarynowski
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
Timothy Spann
 
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdfUdemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Fernanda Palhano
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
Social Samosa
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Aggregage
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
jitskeb
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
nuttdpt
 

Recently uploaded (20)

University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docxDATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
DATA COMMS-NETWORKS YR2 lecture 08 NAT & CLOUD.docx
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
 
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
一比一原版(Unimelb毕业证书)墨尔本大学毕业证如何办理
 
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
原版一比一利兹贝克特大学毕业证(LeedsBeckett毕业证书)如何办理
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
DSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelinesDSSML24_tspann_CodelessGenerativeAIPipelines
DSSML24_tspann_CodelessGenerativeAIPipelines
 
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdfUdemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
 

R Introduction - R Vectors and the Environment

  • 1. R Programming – Introduction and Vectors Ivo Bernardo – Udemy Course
  • 2. The R Language “R is a language and environment for statistical computing and graphics […] R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible.”
  • 3. R Usage - Use as a calculator. - Compute several statistics about data. - Plot data. - Develop machine learning algorithms. Complexity
  • 4. R as a Calculator R performs most of the mathematical calculations you can think of: - 1 + 2 on the console yields 3 - 100-50,6 yields 49.4 - ... R has special functions to compute well known mathematical operations: - sqrt() function computes a square-root. - exp() computes an exponential. - log() computes the logarithm.
  • 5. R as a Calculator You can also perform complex calculations that obey to mathematical rules: - (10/10) / (5/5) = 1 - exp(0) * 20 = 20 - exp(sqrt(9)) = 20.08554
  • 6. R objects Most of the stuff we use in R are objects. These objects have their: - Own set of characteristics (size, dimensions, data types, etc.) - Own set of functions. Example of R objects: - Vector; - Matrix; - List; - DataFrame;
  • 7. R Vectors One of the most simple, yet powerful, object is the vector. - Vectors are characterized by their size and type of the elements. - All elements have to be of the same type in a vector. - Vectors are index-based and you can access the elements by their position (index). - You create a vector with the command c() - To create a vector melons with 4 melons and their weight, in kilograms: - melons <- c(3.4, 3.1, 3, 4.5) - To access the weight of melon 1 we would use melons[1]
  • 8. The Environment Assigning objects into variables using = or <- is the same! melons = c(3.4, 3.1, 3, 4.5) melons <- c(3.4, 3.1, 3, 4.5)
  • 9. Vector Operations It would be good if R would let us make calculations on our vector.. melons <- c(3.4, 3.1, 3, 4.5) “There was a problem when weighting, the melons have only half the weight” melons/2 Each item in the vector is divided by 2! “There was a problem when weighting, the melons have two times the weight” bananas*2 Each item in the vector is multiplied by 2!
  • 10. Vector Operations melons <- c(3.4, 3.1, 3, 4.5) “There was a problem on the measurements, you have to add these values to their weight: 0.4, 0.2, 0.4, 0.3” Melons + c(0.4, 0.2, 0.4, 0.3) Each melon will have it’s weight summed with the corresponding element of the new vector so the resulting vector will be: (3.4+0.4, 3,1+0.2, 3+0.4, 4+0.3) The cool thing is that we can make this more meaningful by calling the c(0.4, 0.2, 0.4, 0.3) vector something related such as adjust_weight and then our calculation could be: new_melons = melons+adjust_weight
  • 11. Vector Operations melons <- c(3.4, 3.1, 3, 4.5) “The value of the melons weight is the square root of the value we gave you” sqrt(melons) “Sum all the melons’ weight please” sum(melons) would yield 14 “What’s the mean of the melons’ length?” mean(melons) would yield 3.5
  • 12. Vector Operations melons <- c(3.4, 3.1, 3, 4.5) “The value of the melons weight is the square root of the value we gave you” sqrt(melons) “Sum all the melons’ weight please” sum(melons) would yield 14 “What’s the mean of the melons’ weight?” mean(melons) would yield 3.5
  • 13. Vector Operations melons <- c(3.4, 3.1, 3, 4.5) R also let you do comparison calculations: “Which Melons weight more than 4 kilograms?” melons>4 This would yield: [FALSE, FALSE, FALSE, TRUE] “Which Melons weight exactly 3 kilograms?” melons==3 This would yield: [FALSE, FALSE, TRUE, FALSE]
  • 14. Vector Operations melons <- c(3.4, 3.1, 3, 4.5) AND and OR are also possible: “Which Melons weight exacly 4 kilograms and 3 kilograms?” melons == 4 & melons== 3 This would yield: [FALSE, FALSE, FALSE, FALSE] “Which Melons weight exacly 4 kilograms or 3 kilograms?” melons == 4 | melons== 3 This would yield: [FALSE, FALSE, TRUE, FALSE]
  • 15. Watch out! When you divide something by 0 you get the symbol meaning infinite: Inf If you use a vector that has an Infinite element in it, our calculations do not stand: sum(c(3.4, 3.1, 3, 4.5/0)) would yield Inf The same happens for NaN (short for Not a Number): sum(c(3.4, 3.1, 3, sqrt(-1)) would yield NaN The same happens for NA (short for Not Available): sum(c(3.4, 3.1, 3, NA)) would yield NA
  • 16. Watch out! - NA’s are pretty common in data analysis so watch out for them. Fortunately, most functions can dodge this by using an extra argument na.rm = TRUE sum(c(3.4, 3.1, 3, NA), na.rm=TRUE) would yield 9.5 mean(c(3.4, 3.1, 3, NA), na.rm=TRUE) would yield 3.166667
  • 17. Want to discover more? Join my R courses on Udemy risk-free (30 day refund policy), where you will have the chance to learn with practical exercises: - R for Absolute Beginners - R for Data Science Looking forward to see you there!