R for Data Analytics (without holiday)
(without holiday)
_____________________________________________
Teaching
Notes for Day1
A.
Teaching Objectives:
Knowledge
Upon completion
of today’s session, the student will:
K-3(1): Know data objects
in R
K-3(2): Understand data
types, conversion methods
K-3(3): General knowledge
of methods, functions and attributes of R objects
K-3(7): Know how to
conditionally link different data frames
S-2(1): Able to check
types and contents of data objects in R
S-2(2): Able to convert
different data objects
S-2(3): Able to apply methods or functions on R
objects to do basic operation
B.
Teaching Topics (Including Examples):
2) Learn how to generate a ‘VECTOR’ with the certain
patterns, extract a sub-vectors based on index and conditions, special values
in R. [App F-K-3(3)] [App F-S-2(2)] [60 min]
3) Learn how to concatenate several vectors. Change
data types, some functions that return vector attributes. [App F-K-3(2,7)] [App
F-S-2(2,3)] [60 min]
Example 1:
Understand Road map for R
Example 2:
Example 3:
Example 4:
Example 5:
Example 6:
C. Class Exercises and Solutions:
a) Class Exercises:
Exercises: (S-2(1, 2)) [60 min]
b) Solutions to Class Exercises:
For Q1 and
Q2 Shown as above
Q3)
|
|
Q4) Solution
|
weight
<- c(71, 67, 83, 67) height
<- c(1.75, 1.81, 1.78, 1.82, 1.97, 2.12, 2.75) bmi <-
weight/height^2 # OUTPUT # Warning
message: # In
weight/height^2 : # longer
object length is not a multiple of shorter object length bmi # R still
carried out the operation, but it recycled the weight object to match the
length # of the
height vector # For the
purposes of this operation, the weight vector looked like this: 71, 67, 83,
67, 71, 67, 83 |
D. Homework Exercises and Solutions:
a)
Homework
Exercises [K-3(2)]
[S-2(2,3)] [60
min]
Problem 3
|
Ø vec
<- c(3000, NA, NA, NA, NA, 4000, 3000, 5000, 1000, 5000) Ø # Extract the
following values from the vec object: Ø #The 6-th value Ø #All the values but
the 2nd one Ø #Values 1, 3, 5, 7,
and 9 Ø #All the values but
the 4th, 5th, and 6th Ø #All the values
larger than 2000 |
Problem 3 SOLUTION
|
vec[6] vec[-2] vec[c(1, 3,
5, 7, 9)] # alternatively vec[seq(1,
9, 2)] # the sequence function
creates a sequence of numbers # starting with
the first element you define, and ending with the second, # with a step
of whatever you pass as a third value # the
corresponding argument names are intuitive: from = , to = , by = vec[-(4:6)] vec[vec
> 2000] # notice that R also
returns the NA elements because it doesn't know their values |
|
# Create a vector called fruits
that stores the following elements:
Apple, Banana , Grapes , Kiwi , avocado # Check the type of fruits # Create a vector called vic
and assign to it the following values: 10, NA, 40 , 30 , 50 , 100 # Check the type of vic. # Use the combine function to combine fruits
and vic
into a single vector called single. # Check the type of the new object. # What conclusions can you draw from this? # If we had added TRUE/FALSE values to the vic
vector, what would have the print(vic) command returned? # What about print(single)? |
Problem 4) Solution
|
Ø fruits = c(
'Apple' , 'Banana' , 'Kiwi' , 'Avacado') Ø typeof(
fruits) Ø vic = c(10,
NA, 40 , 30 , 50 , 100) Ø typeof(vic) Ø single = c(
fruits , vic) Ø typeof(single) Ø single Ø vic = c(10,
NA, 40 , 30 , 50 , 100 , T , F) Ø typeof(vic) Ø #seems like
R has converted all of the double values to characters Ø # this
happens often in R and is done in strict adherence to rules Ø # called
COERCION rules |
C. Class Exercises:
a) Class Exercises:
Exercises: (S-2(1, 2)) [60 min]
|
Ø x =
2*(1:10) Ø x
Ø a =x[8] Ø a
Ø x[-8]
Ø x[2:6]
Ø x[-c(1,6)]
Ø x[-(2:5)]
Ø x[c(5,3,8,3)]
|
|
Ø # get a
sequence number from 1 to 10 Ø x = 1:10 Ø x Ø seq(1:10) Ø # get a sequence
number from 2 to 10 with step 2 Ø seq(from = 2 , to = 10 ,
by = 2) Ø # get 10
replicated 1 Ø rep(1,10) Ø # return by
repeating c(1,2) 5 times Ø y = rep(1:2 , 5) Ø y Ø # repeat each element
twice Ø rep(1:10, each = 2) Ø # character replication
Ø rep(‘a’ , 3) |

|
|
D.
Homework Exercises:
a)
Homework
Exercises [K-3(2)]
[S-2(2,3)] [60
min]

|
Ø
x = 66 Ø
x = paste(x, ‘=65+1’) Ø
x Ø
a = c(1,2,4,8) Ø
a[2] = ‘my name’ Ø
a Ø
b = TRUE Ø
class(b) Ø
b = b + 0 Ø
class(b) Ø
m = as.integer(10) Ø
n = m + 2 Ø
class(n) Ø
o =
as.numeric(m) Ø
class(o) Ø as.character(18) |
|
Ø vec
<- c(3000, NA, NA, NA, NA, 4000, 3000, 5000, 1000, 5000) Ø # Extract the
following values from the vec object:
Ø #The 6-th
value
Ø #All the
values but the 2nd one
Ø #Values 1, 3,
5, 7, and 9
Ø #All the
values but the 4th, 5th, and 6th
Ø
#All the values larger than 2000 |
|
# Create a
vector called fruits that stores the following elements: Apple, Banana , Grapes , Kiwi , avocado # Check
the type of fruits # Create a
vector called vic and assign to it the following values: #10 #NA #40 #30 #50 #100 # Check
the type of vic. # Use the
combine function to combine fruits and vic into a single vector called single. # Check the
type of the new object. # What
conclusions can you draw from this? # If we
had added TRUE/FALSE values to the vic vector, what would have the print(vic) command
returned? # What
about print(single)? |
_____________________________________________
Teaching
Notes for Day2
A.
Teaching Objectives:
Knowledge
Upon completion
of today’s session, the student will:
K-3(1): Know data objects
in R
K-3(2): Understand data
types, conversion methods
K-3(3): General knowledge
of methods, functions and attributes of R objects
S-2(1): Able to check
types and contents of data objects in R
S-2(2): Able to convert
different data objects
S-2(3): Able to apply
methods or functions on R objects to do basic operation
B.
Teaching Topics (Including Examples):
Example 2:
Example 3:
Example 4:
Learn more about ‘MATRIX’ and its computations in R
Example 5:
Learn more about ‘MATRIX’ and its computations in R
Example 6:
Learn more about ‘MATRIX’ and its computations in R
Example 7:
Learn to assign (and refer) column or row names of
matrix objects.
Example 8:
Learn list object in R, create list object, refer
and work on the elements of a list, and manipulate list.
Example 9:
Learn list object in R, create list object, refer
and work on the elements of a list, and manipulate list.
Example 10:
Learn list object in R, create list object, refer
and work on the elements of a list, and manipulate list.
Example 11:
Learn list factor in R, create factor object,
increase or drop a level from a factor, refer and work on the elements of a
list.
Example 12:
Learn list factor in R, create factor object,
increase or drop a level from a factor, refer and work on the elements of a
list.
C. Class Exercises and Solutions:
a) Class Exercises:
Exercises: [60 min]
b) Solutions to Class Exercises:
D. Homework Exercises and Solutions:
a)
Homework
Exercises [60 min]
Problem 1)
Problem 2)
|
MATRIX CREATION #Player Piece
#[1,]
"dark" "king" #[2,]
"dark" "queen" #[3,]
"dark" "pawn" #[4,]
"dark" "pawn" #[5,]
"dark" "knight" #[6,]
"light" "bishop" #[7,]
"light" "king" #[8,]
"light" "rook" #[9,]
"light" "pawn" #[10,]
"light" "pawn" 1. Recreate
this matrix 2. Assign
names to it 3. Transpose
the chess matrix, and add a row storing the following values: 3, 5, 2, 2, 7,
4, 6, 5, 2, 1 4. Name
the row "Turn" and transpose the matrix back to its original
orientation. 5. Extract
the following values from the chess matrix: ·
#The first piece of the light player ·
#The Player and Piece columns ·
#All the information about the dark
player ·
#The Pieces column; try to extract that
as a matrix (Hint: lookup the drop = argument) ·
#Everything but the Piece column ·
#The 1st and 3rd values on the second
row |
Solution to homework for
Problem 2)
|
1 & 2 : player
<- c(rep("dark", 5), rep("light", 5)) piece
<- c("king", "queen", "pawn",
"pawn", "knight", "bishop", "king",
"rook", "pawn", "pawn") chess
<- c(player, piece) chess.mat
<- matrix(chess, nrow = 10, dimnames = list(NULL, c("Player",
"Piece"))) 3 & 4: chess.t
<- t(chess) turn =
c(3,5,2,2,7,4,6,5,2,1) chess.t
<- rbind(chess.t, "Turn" = turn) chess
<- t(chess.t) 5: chess[6,
2] chess[,
1:2] # or chess[,
-3] # or chess[,
c("Player", "Piece")] chess[1:5,
] chess[,
2, drop = FALSE] chess[,
-2] chess[2,
c(1, 3)] chess[7,
3] <- 3 chess[7,
3] |
Problem 3)
|
LIST QUESTION 1. create a list that prints like this: # [[1]] # [1]
1 3 5
7 9 11 # # [[2]] # [1] "Happy Birthday" # # [3] # [1] "Archery" 2. extract the numbers as a vector |
Solution to homework for
Problem 3)
|
1: newList
<- list(seq(1, 11, by = 2), list("Happy Birthday",
"Archery")) newList str(newList) 2:# extract the
numbers as a vector newList[[1]] |
Problem 4)
|
FACTOR QUESTION 1. Save the Piece column of the matrix(you
created above) as a vector. 2. Create a factor from the vector. 3. Organize the levels in the following way
but do not order them: King, Queen, Rook, Bishop, Knight, Pawn. |
Solution to homework for
Problem 4)
|
# the chess matrix #
Player Piece Turn # [1,]
"dark" "king" "3" # [2,]
"dark"
"queen" "5"
# [3,]
"dark" "pawn" "2" # [4,]
"dark" "pawn" "2" # [5,]
"dark" "knight"
"7" # [6,]
"light" "bishop" "4" # [7,]
"light" "king"
"3" # [8,]
"light" "rook"
"5" # [9,]
"light" "pawn"
"2" # [10,] "light"
"pawn" "1" # Save the Piece column of the matrix(you created above)
as a vector piece <- chess[, "Piece"] # Create a factor from the vector. piece <- factor(piece, levels = c("king",
"queen", "rook", "bishop", "knight",
"pawn"), labels = c("King",
"Queen", "Rook", "Bishop", "Knight",
"Pawn")) # Organize the levels in the following way: King, Queen,
Rook, Bishop, Knight, Pawn. levels(piece) <- c("K",
"Q", "R", "B", "K", "P") piece.ordered <- factor(piece, levels =
c("K", "Q", "R", "B", "K",
"P"), labels =
c("King", "Queen", "Rook", "Bishop",
"Knight", "Pawn")) |
|
|
评论
发表评论