Programming With R - Subsets of Data
Programming With R - Subsets of Data
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
Logical/RelaDonal
Operators
Logical/RelaDonal
OperaDons
8/28/16
Examples
> 4 < 3
[1] FALSE
> "a" == "A"
[1] FALSE
> 4
[1]
> 6
[1]
!= 3
TRUE
>= 6
TRUE
TRUE FALSE
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
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
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
8/28/16
rep()
> rep(3,2)
[1] 3 3
> x = c(7,1,3)
> rep(x, 2)
[1] 7 1 3 7 1 3
Repeat
the
vector
2
Dmes
> rep(x, c(3, 2, 1)) Vector
of
reps
for
each
element
[1] 7 7 7 1 1 3
> flastnames =
character(length = length(fbmi))
> flastnames[ ] = Smith