You are on page 1of 77

BA 340 : DATA ANALYTICS

Retrieve and use information in


precise, efficient ways
Question

1. x < - c(1, 2, 5, 1, 4, 9, 4)

y<-x

[1] 1, 2, 5, 1, 4, 9, 1

How can you save just the fifth element of x to


y?

How can you change the fifth element of x to a


0?
Subsetting
Subset notation
Positive Integers

Returns 1 element
Positive Integers

Returns 1 element

Returns more than 1 element


Positive Integers

Returns 1 element at the index 2

Returns elements at the indexes 2 and 4

Returns elements at indexes from 2 to 4


Negative Integers
Negative Integers
Exercise

Fix these poorly written subset commands

vec(1:4)

vec[-1:4]

vec[3, 4, 5]
Blanc Spaces
Names
Names
Logical
Logical
Subset Notation
2D Data Structures
2D Data Structures

Separate
dimensions
with a comma

df[ ?, ?]

Which rows to
Which columns
include
To include
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Positive integers

Positive integers behave just like ij notation in linear


algebra.
Negative integers
Negative integers
Blank Spaces
Blank Spaces
Names

If your object has names, you can ask for elements


or columns back by name.
Names

If your object has names, you can ask for elements


or columns back by name.
Logical
You can subset with a logical vector of the same
length as the dimension you are subsetting.
Each element that corresponds to a TRUE will
be returned.
Logical
You can subset with a logical vector of the same
length as the dimension you are subsetting.
Each element that corresponds to a TRUE will
be returned.
Exercise
Write down as many ways to extract the name
"John" from df as you can. Make sure each
works. You have two minutes.
Answers
Lists
Can you extract the
vector c(1,2) from lst
and run sum on it?

Note that sum


calculates the sum
of a vector
Lists
Can you extract the
vector c(1,2) from lst
and run sum on it?

Note that sum


calculates the sum
of a vector
Lists
Lists
Lists
Lists
To access the entries of a list, we need to use two
brackets [[index]]
Lists

How to access 2nd entry

lst[[1]] returns
Lists

How to access 2nd entry

lst[[1]] returns

Lst[[1]][2] returns 2
$
The most common syntax for subsetting lists and data
frames

names(lst) <- c("alpha", "beta", "gamma")

returns
$

Equivalent to

lst[[‘alpha’]]
$
Logical comparison
c(1, 2, 3, 4, 5) > 3
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)

c(1, 2, 3, 4, 5) >= 3
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)
Returns
c(1, 2, 3, 4, 5) >= 3 c(F, F, T, T, T)
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)
Returns
c(1, 2, 3, 4, 5) >= 3 c(F, F, T, T, T)

c(1, 2, 3, 4, 5) == 3
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)
Returns
c(1, 2, 3, 4, 5) >= 3 c(F, F, T, T, T)

Returns
c(1, 2, 3, 4, 5) == 3 c(F, F, T, F, F)
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)
Returns
c(1, 2, 3, 4, 5) >= 3 c(F, F, T, T, T)

Returns
c(1, 2, 3, 4, 5) == 3 c(F, F, T, F, F)

c(1, 2, 3, 4, 5) != 3
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)
Returns
c(1, 2, 3, 4, 5) >= 3 c(F, F, T, T, T)

Returns
c(1, 2, 3, 4, 5) == 3 c(F, F, T, F, F)

Returns
c(1, 2, 3, 4, 5) != 3 C(T, T, F, T, T)
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)
Returns
c(1, 2, 3, 4, 5) >= 3 c(F, F, T, T, T)

Returns
c(1, 2, 3, 4, 5) == 3 c(F, F, T, F, F)

Returns
c(1, 2, 3, 4, 5) != 3 C(T, T, F, T, T)

c(1, 2, 3, 4, 5) = 3
Logical comparison
Returns
c(1, 2, 3, 4, 5) > 3 c(F, F, F, T, T)
Returns
c(1, 2, 3, 4, 5) >= 3 c(F, F, T, T, T)

Returns
c(1, 2, 3, 4, 5) == 3 c(F, F, T, F, F)
Returns
c(1, 2, 3, 4, 5) != 3 C(T, T, F, T, T)

Returns
Error!! = sign is the same as
c(1, 2, 3, 4, 5) = 3
<-
%in% tests whether the object on the left is a member of the group
on the right.
Boolean operators
Boolean operators
Define :

x <- 3

What is the output of the following expression ?

x<2&x>0
Boolean operators
Define :

x <- 3

What is the output of the following expression ?

x<2&x>0

FALSE & TRUE

FALSE
Boolean operators
Define :

x <- 3

What is the output of the following expression ?

x<2|x>0
Boolean operators
Define :

x <- 3

What is the output of the following expression ?

x<2|x>0

FALSE | TRUE

TRUE
Boolean operators
Define :

x <- 3

What is the output of the following expression ?

xor(x < 2, x > 0)


Boolean operators
Define :

x <- 3

What is the output of the following expression ?

xor(x < 2, x > 0)

xor(FALSE, TRUE)

TRUE
Logical Subsetting
Combining logical tests with subsetting is a very powerful technique!

# What will this return?

x[indices]
Logical Subsetting
Logical Subsetting
NA Behavior
NA Behavior
NA Behavior
NA Behavior
Logical Subsetting

You might also like