SlideShare a Scribd company logo
1 of 25
M.NIVETHITHA,
DEPARTMENT OF INFORMATION TECHNOLOGY,
V.V.VANNIPERUMAL COLLEGE FOR WOMEN,
VIRUDHUNAGAR.
VectorS
Scalars, Vectors,
Arrays, and
Matrices
01
A vector is the simplest type of data
structure in R. Simply put, a vector
is a sequence of data elements of the
same basic type. It is used to store
multiple measurements of the same
type.
(e.g. data variables)
Scalars :
Arrays :
Matrices :
Vectors :
A scalar object is just a single value like a number or a name.
(e.g. a <- 100 b <- 3 / 100 c <- (a + b) / b.)
The array is objects that can hold two or more than two-dimensional data.
A matrix is a two dimensional data set
with columns and rows.
Table of contents
Adding and Deleting
Vector Elements
01 02
Obtaining the
Length of a Vector
03
Matrices and Arrays
as Vectors
Adding and Deleting Vector Elements
• Vectors are stored like arrays in C.
For example,
> x <- c(88,5,12,13)
> x <- c(x[1:3],168,x[4]) # insert 168 before the 13
> x
[1] 88 5 12 168 13
Obtaining the Length of a Vector
You can obtain the length of a vector by using the length() function:
x <- c(1,2,4)
> length(x)
[1] 3
Without the length() function,
for (n in x)
The problem with this approach is that it doesn’t allow us to retrieve the
index of the desired element.
> x <- c()
> x
NULL
> length(x)
[1] 0
> 1:length(x)
[1] 1 0
Matrices and Arrays as Vectors
Arrays and matrices (and even lists, in a sense) are actually vectors too,
For example,
> m
[,1] [,2]
[1,] 1 2
[2,] 3 4
> m + 10:13
[,1] [,2]
[1,] 11 14
[2,] 14 17
Declarations
02
Declare variables in R,
z <- 3
For instance, say we wish y to be a two-component vector with values 5 and 12.
> y[1] <- 5
> y[2] <- 12
> y <- vector(length=2)
> y[1] <- 5
> y[2] <- 12
> y <- c(5,12)
Recycling
03
When applying an operation to two vectors that requires them to be the
same length, R automatically recycles, or repeats, the shorter one, until it is
long enough to match the longer one.
> c(1,2,4) + c(6,0,9,20,22)
[1] 7 2 13 21 24
Here’s a more subtle example:
> x
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> x+ c(1,2)
[,1] [,2]
[1,] 2 6
[2,] 4 6
[3,] 4 8
1 4
2 5
3 6
1 2
2 1
1 2
• x + c(1,2,1,2,1,2)
+
Common Vector
Operations
04
Table of contents
Vector Arithmetic and
Logical Operations
01 02
Vector Indexing
03
Generating Useful Vectors
with the : Operator
Generating Vector
Sequences with seq()
04 05
Repeating Vector
Constants with rep()
Vector Arithmetic and Logical Operations
> 2+3
[1] 5
> "+"(2,3)
[1] 5
> x <- c(1,2,4)
> x + c(5,0,-1)
[1] 6 2 3
> x * c(5,0,-1)
[1] 5 0 -4
> x <- c(1,2,4)
> x / c(5,4,-1)
[1] 0.2 0.5 -4.0
> x %% c(5,4,-1)
[1] 1 2 0
Vector Indexing
One of the most important and frequently used operations in R is that of indexing
vectors.
> y <- c(1.2,3.9,0.4,0.12)
> y[c(1,3)] # extract elements 1 and 3 of y
[1] 1.2 0.4
> y[2:3]
[1] 3.9 0.4
> v <- 3:4
> y[v]
[1] 0.40 0.12
Note that duplicates are allowed.
> x <- c(4,2,17,5)
> y <- x[c(1,1,3)]
> y
[1] 4 4 17
> z <- c(5,12,13)
> z[-1] # exclude element 1
[1] 12 13
> z[-1:-2] # exclude elements 1 through 2
[1] 13
> z <- c(5,12,13)
> z[1:(length(z)-1)]
[1] 5 12
Or
> z[-length(z)]
[1] 5 12
It produces a vector consisting of a range of numbers.
> 5:8
[1] 5 6 7 8
> 5:1
[1] 5 4 3 2 1
> i <- 2
> 1:i-1 # this means (1:i) - 1, not 1:(i-1)
[1] 0 1
> 1:(i-1)
[1] 1
Generating Useful Vectors with the : Operator
A generalization of : is the seq() (or sequence) function, which generates a sequence in
arithmetic progression.
> seq(from=12,to=30,by=3)
[1] 12 15 18 21 24 27 30
> x <- c(5,12,13)
> x
[1] 5 12 13
> seq(x)
[1] 1 2 3
> x <- NULL
> x
NULL
> seq(x)
integer(0)
Generating Vector Sequences with seq()
The rep() (or repeat) function allows us to conveniently put the same con-
stant into long vectors. The call form is,
rep(x,times)
> x <- rep(8,4)
> x
[1] 8 8 8 8
> rep(c(5,12,13),3)
[1] 5 12 13 5 12 13 5 12 13
> rep(1:3,2)
[1] 1 2 3 1 2 3
> rep(c(5,12,13),each=2)
[1] 5 5 12 12 13 13
Repeating Vector Constants with rep()
Filtering
05
Table of contents
Generating Filtering
Indices
01 02
Filtering with the subset()
Function
03
The Selection Function
which()
Generating Filtering Indices
Another feature reflecting the functional language nature of R is filtering.
This allows us to extract a vector’s elements that satisfy certain
conditions.
> z <- c(5,2,-3,8)
> z
[1] 5 2 -3 8
> z*z>8
[1] TRUE FALSE TRUE TRUE
> z <- c(5,2,-3,8)
> y <- c(1,2,30,5)
> y[z*z > 8]
[1] 1 30 5
> x <- c(1,3,8,2,20)
> x[x > 3] <- 0
> x
[1] 1 3 0 2 0
Filtering with the subset() Function
Filtering can also be done with the subset() function. When applied to vectors, the
difference between using this function and ordinary filtering lies in the manner in
which NA values are handled.
> x <- c(6,1:3,NA,12)
> x
[1] 6 1 2 3 NA 12
> x[x > 5]
[1] 6 NA 12
> subset(x,x > 5)
[1] 6 12
The Selection Function which()
> z <- c(5,2,-3,8)
> which(z*z > 8)
[1] 1 3 4
To find the positions within z at which the condition occurs.
Vectors.pptx

More Related Content

Similar to Vectors.pptx

Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptxAdrien Melquiond
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
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
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environmentYogendra Chaubey
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
 
Matlab Overviiew 2
Matlab Overviiew 2Matlab Overviiew 2
Matlab Overviiew 2Nazim Naeem
 
Matlab ch1 (3)
Matlab ch1 (3)Matlab ch1 (3)
Matlab ch1 (3)mohsinggg
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2Kevin Chun-Hsien Hsu
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmitabeasiswa
 
Introduction to R r.nabati - iausdj.ac.ir
Introduction to R   r.nabati - iausdj.ac.irIntroduction to R   r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.irnabati
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
Day 1d R structures & objects: matrices and data frames.pptx
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptxAdrien Melquiond
 

Similar to Vectors.pptx (20)

Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
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
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
Vector in R
Vector in RVector in R
Vector in R
 
bobok
bobokbobok
bobok
 
Lecture two
Lecture twoLecture two
Lecture two
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Matlab
MatlabMatlab
Matlab
 
Matlab Overviiew 2
Matlab Overviiew 2Matlab Overviiew 2
Matlab Overviiew 2
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
Matlab ch1 (3)
Matlab ch1 (3)Matlab ch1 (3)
Matlab ch1 (3)
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Introduction to R r.nabati - iausdj.ac.ir
Introduction to R   r.nabati - iausdj.ac.irIntroduction to R   r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.ir
 
Matlab Tutorial
Matlab TutorialMatlab Tutorial
Matlab Tutorial
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
Day 1d R structures & objects: matrices and data frames.pptx
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptx
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Vectors.pptx

  • 1. M.NIVETHITHA, DEPARTMENT OF INFORMATION TECHNOLOGY, V.V.VANNIPERUMAL COLLEGE FOR WOMEN, VIRUDHUNAGAR. VectorS
  • 3. A vector is the simplest type of data structure in R. Simply put, a vector is a sequence of data elements of the same basic type. It is used to store multiple measurements of the same type. (e.g. data variables) Scalars : Arrays : Matrices : Vectors : A scalar object is just a single value like a number or a name. (e.g. a <- 100 b <- 3 / 100 c <- (a + b) / b.) The array is objects that can hold two or more than two-dimensional data. A matrix is a two dimensional data set with columns and rows.
  • 4. Table of contents Adding and Deleting Vector Elements 01 02 Obtaining the Length of a Vector 03 Matrices and Arrays as Vectors
  • 5. Adding and Deleting Vector Elements • Vectors are stored like arrays in C. For example, > x <- c(88,5,12,13) > x <- c(x[1:3],168,x[4]) # insert 168 before the 13 > x [1] 88 5 12 168 13
  • 6. Obtaining the Length of a Vector You can obtain the length of a vector by using the length() function: x <- c(1,2,4) > length(x) [1] 3 Without the length() function, for (n in x) The problem with this approach is that it doesn’t allow us to retrieve the index of the desired element. > x <- c() > x NULL > length(x) [1] 0 > 1:length(x) [1] 1 0
  • 7. Matrices and Arrays as Vectors Arrays and matrices (and even lists, in a sense) are actually vectors too, For example, > m [,1] [,2] [1,] 1 2 [2,] 3 4 > m + 10:13 [,1] [,2] [1,] 11 14 [2,] 14 17
  • 9. Declare variables in R, z <- 3 For instance, say we wish y to be a two-component vector with values 5 and 12. > y[1] <- 5 > y[2] <- 12 > y <- vector(length=2) > y[1] <- 5 > y[2] <- 12 > y <- c(5,12)
  • 11. When applying an operation to two vectors that requires them to be the same length, R automatically recycles, or repeats, the shorter one, until it is long enough to match the longer one. > c(1,2,4) + c(6,0,9,20,22) [1] 7 2 13 21 24 Here’s a more subtle example: > x [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > x+ c(1,2) [,1] [,2] [1,] 2 6 [2,] 4 6 [3,] 4 8 1 4 2 5 3 6 1 2 2 1 1 2 • x + c(1,2,1,2,1,2) +
  • 13. Table of contents Vector Arithmetic and Logical Operations 01 02 Vector Indexing 03 Generating Useful Vectors with the : Operator Generating Vector Sequences with seq() 04 05 Repeating Vector Constants with rep()
  • 14. Vector Arithmetic and Logical Operations > 2+3 [1] 5 > "+"(2,3) [1] 5 > x <- c(1,2,4) > x + c(5,0,-1) [1] 6 2 3 > x * c(5,0,-1) [1] 5 0 -4 > x <- c(1,2,4) > x / c(5,4,-1) [1] 0.2 0.5 -4.0 > x %% c(5,4,-1) [1] 1 2 0
  • 15. Vector Indexing One of the most important and frequently used operations in R is that of indexing vectors. > y <- c(1.2,3.9,0.4,0.12) > y[c(1,3)] # extract elements 1 and 3 of y [1] 1.2 0.4 > y[2:3] [1] 3.9 0.4 > v <- 3:4 > y[v] [1] 0.40 0.12 Note that duplicates are allowed. > x <- c(4,2,17,5) > y <- x[c(1,1,3)] > y [1] 4 4 17
  • 16. > z <- c(5,12,13) > z[-1] # exclude element 1 [1] 12 13 > z[-1:-2] # exclude elements 1 through 2 [1] 13 > z <- c(5,12,13) > z[1:(length(z)-1)] [1] 5 12 Or > z[-length(z)] [1] 5 12
  • 17. It produces a vector consisting of a range of numbers. > 5:8 [1] 5 6 7 8 > 5:1 [1] 5 4 3 2 1 > i <- 2 > 1:i-1 # this means (1:i) - 1, not 1:(i-1) [1] 0 1 > 1:(i-1) [1] 1 Generating Useful Vectors with the : Operator
  • 18. A generalization of : is the seq() (or sequence) function, which generates a sequence in arithmetic progression. > seq(from=12,to=30,by=3) [1] 12 15 18 21 24 27 30 > x <- c(5,12,13) > x [1] 5 12 13 > seq(x) [1] 1 2 3 > x <- NULL > x NULL > seq(x) integer(0) Generating Vector Sequences with seq()
  • 19. The rep() (or repeat) function allows us to conveniently put the same con- stant into long vectors. The call form is, rep(x,times) > x <- rep(8,4) > x [1] 8 8 8 8 > rep(c(5,12,13),3) [1] 5 12 13 5 12 13 5 12 13 > rep(1:3,2) [1] 1 2 3 1 2 3 > rep(c(5,12,13),each=2) [1] 5 5 12 12 13 13 Repeating Vector Constants with rep()
  • 21. Table of contents Generating Filtering Indices 01 02 Filtering with the subset() Function 03 The Selection Function which()
  • 22. Generating Filtering Indices Another feature reflecting the functional language nature of R is filtering. This allows us to extract a vector’s elements that satisfy certain conditions. > z <- c(5,2,-3,8) > z [1] 5 2 -3 8 > z*z>8 [1] TRUE FALSE TRUE TRUE > z <- c(5,2,-3,8) > y <- c(1,2,30,5) > y[z*z > 8] [1] 1 30 5 > x <- c(1,3,8,2,20) > x[x > 3] <- 0 > x [1] 1 3 0 2 0
  • 23. Filtering with the subset() Function Filtering can also be done with the subset() function. When applied to vectors, the difference between using this function and ordinary filtering lies in the manner in which NA values are handled. > x <- c(6,1:3,NA,12) > x [1] 6 1 2 3 NA 12 > x[x > 5] [1] 6 NA 12 > subset(x,x > 5) [1] 6 12
  • 24. The Selection Function which() > z <- c(5,2,-3,8) > which(z*z > 8) [1] 1 3 4 To find the positions within z at which the condition occurs.