You are on page 1of 7

8/28/16

5 Ways to Subset
Logical logical vector where TRUEs denote the
elements in the subset
Posi+on integer vector with the posiDons of the
elements in the subset
Exclusion vector of negaDve integers for
posiDons to exclude from subset
Name character vector of names of elements in
the subset (object must have named elements)
All all elements are in the subset!

Subse,ng

Subset by logical
logical
T
F
T
F
F
F
T
T
F
F
F
F
F
F

Subset by posiDon

age
73
33
79
47
27
33
67
52
59
27
55
24
46
48

age
subset

position
1

73

79

67

52

73
33
79
47
27
33
67
52
59
27
55
24
46
48

subset
73
79
67
52

8/28/16

Subset by posiDon

Subset by exclusion

age
position

age
subset

73
33
79
47
27
33
67
52
59
27
55
24
46
48

1
8
7
3

73
52
67
79

"a"
"c"
"g"
"h"

name
"a"
"b"
"c"
"d"
"e"
"f"
"g"
"h"
"i"
"j"
"k"
"l"
"m"
"n"

age
73
33
79
47
27
33
67
52
59
27
55
24
46
48

-2
-4
-5
-6
-9
-10
-11
-12
-13
-14

Subset by name
name

exclusion

subset
73
79
67
52

73
33
79
47
27
33
67
52
59
27
55
24
46
48

subset
73
79
67
52

Examples of Subsets
1. Ages of those who are not overweight
2. Weights of women in the family
3. Genders of those over 50 years old and
under 70 inches tall
4. BMI of the tallest member of the family
5. Height elements a, c, f
6. BMI of every other person in the family
7. New vector of last names all Smith

8/28/16

1. Ages of overweight family members


Ages of the overweight family members
> fage[foverWt]
[1] 77 33 67 59 27 55

2. Weights of women in the family


Create a logical expression that idenDes the
women in the family
> fsex == "f"
[1] FALSE TRUE FALSE FALSE TRUE TRUE FALSE
[8] TRUE FALSE FALSE TRUE FALSE FALSE TRUE

Ages of those who are not over weight


> fage[!foverWt]
This is
[1] 33 79 47 27 52 24 46 48 Boolean
Algebra

Use this logical expression to subset the


vector fweight
This is a
> fweight[fsex == "f"]
RelaDonal
[1] 124 98 190 124 166 125
OperaDon

Logical/RelaDonal Operators
Logical/RelaDonal OperaDons

In addiDon to operators such as +, -, *,


and /, R also has logical operators
These are relaDonal operators
>, <, >=, <=, !=, and ==
These return logical values (TRUE or FALSE)
They are also vectorized operaDons

8/28/16

Examples
> 4 < 3
[1] FALSE
> "a" == "A"
[1] FALSE

> 4
[1]

> 6
[1]

!= 3
TRUE
>= 6
TRUE

> fweight > 150


[1] TRUE FALSE TRUE TRUE FALSE TRUE
[9] TRUE TRUE TRUE FALSE FALSE FALSE

TRUE FALSE

> fsex !="m"


[1] FALSE TRUE FALSE FALSE TRUE TRUE FALSE
[9] FALSE FALSE TRUE FALSE FALSE TRUE

TRUE

> fbmi
[1] 25.16239 21.50106 24.45884 24.48414 18.51492
[6] 28.94981 28.18797 20.67783 26.66430 30.04911
[11] 26.05364 22.64384 24.26126 22.91060

> fbmi == 25.16239


[1] FALSE FALSE FALSE FALSE FALSE FALSE

What is printed at the console


need not be idenDcal to the
numeric value stored

3. Genders of those over 50 and


shorter than 70
Genders of the family members who are
over 50
fsex[fage > 50]
[1] m m m f m f
Levels: f m

Genders of those over 50 years old and


under 70 inches tall
> fsex[fage > 50 & fheight < 70]
[1] m f m f
Levels: f m

Boolean Algebra
Boolean algebra is a mathemaDcal formalizaDon of
the truth or falsity of statements.
It has three operaDons, not, or, and and.
Boolean algebra tells us how to evaluate the truth
or falsity of compound statements that are built
using these operaDons. For example, if A and B are
statements, some compound statements are
A and B
(not A) or B

8/28/16

Two other funcDons


The not operaDon just causes the statement
following it to switch its truth value.
So not TRUE is FALSE and not FALSE is TRUE.
The compound statement A and B is TRUE only if
both A and B are TRUE.
The compound statement A or B is TRUE if either
or both A or B is TRUE.
In R, we write ! for not, & for and, and | for
or. Note: all of these are vectorized!

Two other useful funcDons that operate on


logical vectors are all and any.
Can you guess what they do?
> all(fage > 18)
[1] TRUE
> any(fage < 18)
[1] FALSE
> any(fweight < 150)
[1] TRUE
> all(fweight < 150)
[1] FALSE

4. BMI of the tallest family member


Greatest height
> max(fheight)
[1] 73

CreaDng vectors


Element in eeight that matches the tallest
> fbmi[fheight == max(fheight)]
c
24.45884

8/28/16

concatenate
> c(3, 2, 1)
[1] 3 2 1
> c(bob =3, alice = 2,
john = 1)
bob alice john
3 2 1

A vector of three
numbers, 3, 2, 1, in that
order
Elements in a vector
this Dme with names

5. Height elements a, c, f
> fheight[c(a, c, f)]
a c f
70 73 68
> fheight[c(a, f, f, c)]
a f c f
Note: Order of names
70 68 73 68

determines order in subset


If we repeat a name we get
the element mulDple Dmes

1:3 returns a numeric vector of


1-apart values
> 1:3
[1] 1 2 3
> 10:6
[1] 10 9 8 7 6
> 1.1:5.7
[1] 1.1 2.1 3.1 4.1 5.1
> 5.7:-1.1
[1] 5.7 4.7 3.7 2.7 1.7 0.7 -0.3

seq() a richer version of :


> seq(1, 6, by = 2)
[1] 1 3 5
> seq(1, 6, length = 3)
[1] 1.0 3.5 6.0
> seq(to = 6, length = 3, by
= 2)
[1] 2 4 6
> seq(from = 1, length = 3,
by = 2)
[1] 1 3 5

seq() has several


arguments
from
to
by
length
There are many ways to
call this funcDon

8/28/16

6. BMI of every other person


> fbmi[seq(from = 1, to = length(fbmi),
by = 2)]
a
c
e
g
i
k
m
25.1 24.4 18.5 28.1 26.6 26.0 24.2

rep()
> rep(3,2)
[1] 3 3

Vector of two threes

> x = c(7,1,3)
> rep(x, 2)
[1] 7 1 3 7 1 3


Repeat the vector 2 Dmes

> rep(x, each = 2)


[1] 7 7 1 1 3 3

The each argument


repeats each element 2
Dmes


> rep(x, c(3, 2, 1)) Vector of reps for each
element
[1] 7 7 7 1 1 3

7. Vector of last names


> flastnames = rep(Smith,
times = length(fbmi))

> flastnames =
character(length = length(fbmi))
> flastnames[ ] = Smith

You might also like