SlideShare una empresa de Scribd logo
1 de 12
Vector
Vector
• A vector is a sequence of data elements of the same basic type. Members in a
vector are officially called components. Nevertheless, we will just call
them members in this site.
• Numeric : c(1,2,3)
• Logical : c(TRUE,FALSE,TRUE)
• Character : c(“xyz”,”zz”,”EWQ”)
• Length :The number of elements or members in a vector is given by function
length
• > length(c("aa", "bb", "cc", "dd", "ee"))
[1] 5
Combining Vectors
• Vectors can be combined via the function c. For examples, the following two
vectors n ands are combined into a new vector containing elements from both
vectors.
• > n = c(2, 3, 5)
> s = c("aa", "bb", "cc", "dd", "ee")
> c(n, s)
[1] "2" "3" "5" "aa" "bb" "cc" "dd" "ee“
• Value Coercion
In the code snippet above, notice how the numeric values are being coerced into
character strings when the two vectors are combined. This is necessary so as to
maintain the same primitive data type for members in the same vector.
Vector Arithmetic
• Arithmetic operations of vectors are performed member-by-member, i.e., memberwise.
• For example, suppose we have two vectors a and b.
> a = c(1, 3, 5, 7)
> b = c(1, 2, 4, 8)
 Multiply
> 5 * a
[1] 5 15 25 35
 Addition
> a + b
[1] 2 5 9 15
 Subtraction
> a - b
[1] 0 1 1 -1
 Multplication
> a * b
[1] 1 6 20 56
 Division
> a / b
[1] 1.000 1.500 1.250 0.875
Recycling rule
• If two vectors are of unequal length, the shorter one will be recycled in order
to match the longer vector. For example, the following vectors u and v have
different lengths, and their sum is computed by recycling values of the shorter
vector u.
• > u = c(10, 20, 30)
> v = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
> u + v
[1] 11 22 33 14 25 36 17 28 39
Vector index
• We retrieve values in a vector by declaring an index inside a single square
bracket "[]"operator.
• For example, the following shows how to retrieve a vector member. Since the
vector index is 1-based, we use the index position 3 for retrieving the third
member.
• > s = c("aa", "bb", "cc", "dd", "ee")
> s[3]
[1] "cc"
• Unlike other programming languages, the square bracket operator returns more
than just individual members. In fact, the result of the square bracket operator is
another vector, and s[3] is a vector slice containing a single member "cc".
Negative index
• If the index is negative, it would strip the member whose position has the same
absolute value as the negative index. For example, the following creates a vector
slice with the third member removed.
• > s[-3]
[1] "aa" "bb" "dd" "ee"
Out-of-Range Index
• If an index is out-of-range, a missing value will be reported via the symbol NA.
• > s[10]
[1] NA
Numeric Index Vector
• A new vector can be sliced from a given vector with a numeric index vector,
which consists of member positions of the original vector to be retrieved.
• Here it shows how to retrieve a vector slice containing the second and third
members of a given vector s.
• > s = c("aa", "bb", "cc", "dd", "ee")
> s[c(2, 3)]
[1] "bb" "cc"
Duplicate Indexes
• The index vector allows duplicate values. Hence the following retrieves a member
twice in one operation.
• > s[c(2, 3, 3)]
[1] "bb" "cc" "cc"
• Outer-of-order-indexes
• The index vector can even be out-of-order. Here is a vector slice with the order of
first and second members reversed.
• > s[c(2, 1, 3)]
[1] "bb" "aa" "cc"
• Range index
• To produce a vector slice between two indexes, we can use the colon operator ":".
This can be convenient for situations involving large vectors.
• > s[2:4]
[1] "bb" "cc" "dd"
Logical Index Vector
• A new vector can be sliced from a given vector with a logical index vector, which
has the same length as the original vector. Its members are TRUE if the
corresponding members in the original vector are to be included in the slice,
and FALSE if otherwise.
• For example, consider the following vector s of length 5.
• > s = c("aa", "bb", "cc", "dd", "ee")
• To retrieve the the second and fourth members of s, we define a logical vector L of
the same length, and have its second and fourth members set as TRUE.
• > L = c(FALSE, TRUE, FALSE, TRUE, FALSE)
> s[L]
[1] "bb" "dd"
• The code can be abbreviated into a single line.
• > s[c(FALSE, TRUE, FALSE, TRUE, FALSE)]
[1] "bb" "dd"
Named Vector Members
• We can assign names to vector members.
• For example, the following variable v is a character string vector with two
members.
• > v = c("Mary", "Sue")
> v
[1] "Mary" "Sue"
• We now name the first member as First, and the second as Last.
• > names(v) = c("First", "Last")
> v
First Last
"Mary" "Sue"
• Then we can retrieve the first member by its name.
• > v["First"]
[1] "Mary"
• Furthermore, we can reverse the order with a character string index vector.
• > v[c("Last", "First")]
Last First
"Sue" "Mary"

Más contenido relacionado

La actualidad más candente

Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language CHANDAN KUMAR
 
R Programming Language
R Programming LanguageR Programming Language
R Programming LanguageNareshKarela1
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data TypesRsquared Academy
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithmAamir Sohail
 
Exploratory data analysis
Exploratory data analysis Exploratory data analysis
Exploratory data analysis Peter Reimann
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with RShareThis
 
Data visualization using R
Data visualization using RData visualization using R
Data visualization using RUmmiya Mohammedi
 
R language tutorial
R language tutorialR language tutorial
R language tutorialDavid Chiu
 
Import Data using R
Import Data using R Import Data using R
Import Data using R Rupak Roy
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Guy Lebanon
 

La actualidad más candente (20)

Data structures chapter 1
Data structures chapter  1Data structures chapter  1
Data structures chapter 1
 
Chart and graphs in R programming language
Chart and graphs in R programming language Chart and graphs in R programming language
Chart and graphs in R programming language
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
 
Step By Step Guide to Learn R
Step By Step Guide to Learn RStep By Step Guide to Learn R
Step By Step Guide to Learn R
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
6. R data structures
6. R data structures6. R data structures
6. R data structures
 
Exploratory data analysis
Exploratory data analysis Exploratory data analysis
Exploratory data analysis
 
R programming
R programmingR programming
R programming
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
 
Data visualization using R
Data visualization using RData visualization using R
Data visualization using R
 
R data types
R   data typesR   data types
R data types
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
Data frame operations
Data frame operationsData frame operations
Data frame operations
 
Import Data using R
Import Data using R Import Data using R
Import Data using R
 
Data Analysis with R (combined slides)
Data Analysis with R (combined slides)Data Analysis with R (combined slides)
Data Analysis with R (combined slides)
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 

Destacado

Newton First Law Of Motion
Newton First Law Of MotionNewton First Law Of Motion
Newton First Law Of MotionMLFGarcia
 
Motion laws of newton 1
Motion laws of newton 1Motion laws of newton 1
Motion laws of newton 1mstf mstf
 
3 newton law of motion-Laws of Newtonian mechanics
3 newton law of motion-Laws of Newtonian mechanics 3 newton law of motion-Laws of Newtonian mechanics
3 newton law of motion-Laws of Newtonian mechanics UET Lahore
 
Motion laws of newton 2
Motion laws of newton 2Motion laws of newton 2
Motion laws of newton 2mstf mstf
 
newton law
newton lawnewton law
newton law2461998
 
6 newton law and gravitation
6  newton law and gravitation6  newton law and gravitation
6 newton law and gravitationGalih Suryono
 
Newton law ppt
Newton law pptNewton law ppt
Newton law pptruthkezia
 
PPT Newton 3rd law
PPT Newton 3rd lawPPT Newton 3rd law
PPT Newton 3rd lawArun Murali
 
Dynamics13lecture
Dynamics13lectureDynamics13lecture
Dynamics13lectureAbdou Secka
 
Mechanics ppt 1
Mechanics ppt 1Mechanics ppt 1
Mechanics ppt 1IB School
 
mechanic of machine-Mechanical Vibrations
mechanic of machine-Mechanical Vibrationsmechanic of machine-Mechanical Vibrations
mechanic of machine-Mechanical VibrationsYarwindran Mogan
 
Chapter 11 kinematics of particles
Chapter 11 kinematics of particlesChapter 11 kinematics of particles
Chapter 11 kinematics of particlesRogin Beldeneza
 
NEWTONS LAW AND INERTIA
NEWTONS LAW AND INERTIANEWTONS LAW AND INERTIA
NEWTONS LAW AND INERTIAAMMU99
 
Newtons laws of_motion - 1st law
Newtons laws of_motion - 1st lawNewtons laws of_motion - 1st law
Newtons laws of_motion - 1st lawTekZeno
 
Law of Inertia and Running Starts
Law of Inertia and Running StartsLaw of Inertia and Running Starts
Law of Inertia and Running StartsJan Parker
 

Destacado (20)

Newton First Law Of Motion
Newton First Law Of MotionNewton First Law Of Motion
Newton First Law Of Motion
 
BASIC VECTOR NOTES
BASIC VECTOR NOTESBASIC VECTOR NOTES
BASIC VECTOR NOTES
 
Motion laws of newton 1
Motion laws of newton 1Motion laws of newton 1
Motion laws of newton 1
 
3 newton law of motion-Laws of Newtonian mechanics
3 newton law of motion-Laws of Newtonian mechanics 3 newton law of motion-Laws of Newtonian mechanics
3 newton law of motion-Laws of Newtonian mechanics
 
Motion laws of newton 2
Motion laws of newton 2Motion laws of newton 2
Motion laws of newton 2
 
newton law
newton lawnewton law
newton law
 
6 newton law and gravitation
6  newton law and gravitation6  newton law and gravitation
6 newton law and gravitation
 
Newton law ppt
Newton law pptNewton law ppt
Newton law ppt
 
PPT Newton 3rd law
PPT Newton 3rd lawPPT Newton 3rd law
PPT Newton 3rd law
 
Dynamics13lecture
Dynamics13lectureDynamics13lecture
Dynamics13lecture
 
Mechanics ppt 1
Mechanics ppt 1Mechanics ppt 1
Mechanics ppt 1
 
mechanic of machine-Mechanical Vibrations
mechanic of machine-Mechanical Vibrationsmechanic of machine-Mechanical Vibrations
mechanic of machine-Mechanical Vibrations
 
Chapter 11 kinematics of particles
Chapter 11 kinematics of particlesChapter 11 kinematics of particles
Chapter 11 kinematics of particles
 
Chapter 1(4)SCALAR AND VECTOR
Chapter 1(4)SCALAR AND VECTORChapter 1(4)SCALAR AND VECTOR
Chapter 1(4)SCALAR AND VECTOR
 
Newton%92s first law
Newton%92s first lawNewton%92s first law
Newton%92s first law
 
6 ohm's law
6 ohm's law6 ohm's law
6 ohm's law
 
NEWTONS LAW AND INERTIA
NEWTONS LAW AND INERTIANEWTONS LAW AND INERTIA
NEWTONS LAW AND INERTIA
 
Newtons laws of_motion - 1st law
Newtons laws of_motion - 1st lawNewtons laws of_motion - 1st law
Newtons laws of_motion - 1st law
 
Showable
ShowableShowable
Showable
 
Law of Inertia and Running Starts
Law of Inertia and Running StartsLaw of Inertia and Running Starts
Law of Inertia and Running Starts
 

Similar a Vector in R

8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data framesExternalEvents
 
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdfIntroduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdfYasirMuhammadlawan
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data framesFAO
 
Matlab Overviiew 2
Matlab Overviiew 2Matlab Overviiew 2
Matlab Overviiew 2Nazim Naeem
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdfMariappanR3
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxrohinitalekar1
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlabTUOS-Sam
 
Matlab ch1 (3)
Matlab ch1 (3)Matlab ch1 (3)
Matlab ch1 (3)mohsinggg
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functionsjoellivz
 

Similar a Vector in R (20)

Vectormaths and Matrix in R.pptx
Vectormaths and Matrix in R.pptxVectormaths and Matrix in R.pptx
Vectormaths and Matrix in R.pptx
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
 
Vectors.pptx
Vectors.pptxVectors.pptx
Vectors.pptx
 
R Basics
R BasicsR Basics
R Basics
 
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdfIntroduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
Introduction to matlab chapter2 by Dr.Bashir m. sa'ad.pdf
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
Matlab Overviiew 2
Matlab Overviiew 2Matlab Overviiew 2
Matlab Overviiew 2
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
 
Matlab ch1 (3)
Matlab ch1 (3)Matlab ch1 (3)
Matlab ch1 (3)
 
working with matrices in r
working with matrices in rworking with matrices in r
working with matrices in r
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 

Último

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfEr. Suman Jyoti
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 

Último (20)

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 

Vector in R

  • 2. Vector • A vector is a sequence of data elements of the same basic type. Members in a vector are officially called components. Nevertheless, we will just call them members in this site. • Numeric : c(1,2,3) • Logical : c(TRUE,FALSE,TRUE) • Character : c(“xyz”,”zz”,”EWQ”) • Length :The number of elements or members in a vector is given by function length • > length(c("aa", "bb", "cc", "dd", "ee")) [1] 5
  • 3. Combining Vectors • Vectors can be combined via the function c. For examples, the following two vectors n ands are combined into a new vector containing elements from both vectors. • > n = c(2, 3, 5) > s = c("aa", "bb", "cc", "dd", "ee") > c(n, s) [1] "2" "3" "5" "aa" "bb" "cc" "dd" "ee“ • Value Coercion In the code snippet above, notice how the numeric values are being coerced into character strings when the two vectors are combined. This is necessary so as to maintain the same primitive data type for members in the same vector.
  • 4. Vector Arithmetic • Arithmetic operations of vectors are performed member-by-member, i.e., memberwise. • For example, suppose we have two vectors a and b. > a = c(1, 3, 5, 7) > b = c(1, 2, 4, 8)  Multiply > 5 * a [1] 5 15 25 35  Addition > a + b [1] 2 5 9 15  Subtraction > a - b [1] 0 1 1 -1  Multplication > a * b [1] 1 6 20 56  Division > a / b [1] 1.000 1.500 1.250 0.875
  • 5. Recycling rule • If two vectors are of unequal length, the shorter one will be recycled in order to match the longer vector. For example, the following vectors u and v have different lengths, and their sum is computed by recycling values of the shorter vector u. • > u = c(10, 20, 30) > v = c(1, 2, 3, 4, 5, 6, 7, 8, 9) > u + v [1] 11 22 33 14 25 36 17 28 39
  • 6. Vector index • We retrieve values in a vector by declaring an index inside a single square bracket "[]"operator. • For example, the following shows how to retrieve a vector member. Since the vector index is 1-based, we use the index position 3 for retrieving the third member. • > s = c("aa", "bb", "cc", "dd", "ee") > s[3] [1] "cc" • Unlike other programming languages, the square bracket operator returns more than just individual members. In fact, the result of the square bracket operator is another vector, and s[3] is a vector slice containing a single member "cc".
  • 7. Negative index • If the index is negative, it would strip the member whose position has the same absolute value as the negative index. For example, the following creates a vector slice with the third member removed. • > s[-3] [1] "aa" "bb" "dd" "ee"
  • 8. Out-of-Range Index • If an index is out-of-range, a missing value will be reported via the symbol NA. • > s[10] [1] NA
  • 9. Numeric Index Vector • A new vector can be sliced from a given vector with a numeric index vector, which consists of member positions of the original vector to be retrieved. • Here it shows how to retrieve a vector slice containing the second and third members of a given vector s. • > s = c("aa", "bb", "cc", "dd", "ee") > s[c(2, 3)] [1] "bb" "cc" Duplicate Indexes • The index vector allows duplicate values. Hence the following retrieves a member twice in one operation. • > s[c(2, 3, 3)] [1] "bb" "cc" "cc" • Outer-of-order-indexes • The index vector can even be out-of-order. Here is a vector slice with the order of first and second members reversed. • > s[c(2, 1, 3)] [1] "bb" "aa" "cc"
  • 10. • Range index • To produce a vector slice between two indexes, we can use the colon operator ":". This can be convenient for situations involving large vectors. • > s[2:4] [1] "bb" "cc" "dd"
  • 11. Logical Index Vector • A new vector can be sliced from a given vector with a logical index vector, which has the same length as the original vector. Its members are TRUE if the corresponding members in the original vector are to be included in the slice, and FALSE if otherwise. • For example, consider the following vector s of length 5. • > s = c("aa", "bb", "cc", "dd", "ee") • To retrieve the the second and fourth members of s, we define a logical vector L of the same length, and have its second and fourth members set as TRUE. • > L = c(FALSE, TRUE, FALSE, TRUE, FALSE) > s[L] [1] "bb" "dd" • The code can be abbreviated into a single line. • > s[c(FALSE, TRUE, FALSE, TRUE, FALSE)] [1] "bb" "dd"
  • 12. Named Vector Members • We can assign names to vector members. • For example, the following variable v is a character string vector with two members. • > v = c("Mary", "Sue") > v [1] "Mary" "Sue" • We now name the first member as First, and the second as Last. • > names(v) = c("First", "Last") > v First Last "Mary" "Sue" • Then we can retrieve the first member by its name. • > v["First"] [1] "Mary" • Furthermore, we can reverse the order with a character string index vector. • > v[c("Last", "First")] Last First "Sue" "Mary"