R for Data Analytics (without holiday)

 

 R for Data Analytics

(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

 

Skill

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):

 

Introducing R Background, Console and Environment; Introducing Learning Roadmap

1)      Understand R VARIABLE and learn how to create and use R variables. Learn R objects that can hold multiple values, ATOMIC DATA TYPES in R. Learn R packages and usages. [App F-K-3(1,2)] [App F-S-2(1,2)] [90 min]

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]

4)      Classroom exercises [90 min]

Example 1:

Understand Road map for R

 

Example 2:

R Environment – R console

Example 3:

R Environment – R console, understand R VARIABLE

 

Example 4:

 

R Environment – R console, understand R VARIABLE

 


 

Example 5:

 

Learn R objects that can hold multiple values, ATOMIC DATA TYPES in R

 

 

 

 

 

 

 

 

 

 

 

Example 6:

Learn R objects that can hold multiple values, ATOMIC DATA TYPES in R

 

 

 

 

 

 

 

 

 

 

Example 7:

Generate vector with pattern  

 

 

 

 

 

Example 8:

Extract sub-vector

 


 

Example 9:

Conditionally extract vector

 

 

 

 

 

 

 

 

 

 

 

 

Example 10:

Concatenate vectors

 

Example 11:

Return the length of vectors

Example 12:

Data types and conversion

 


 

Example 13:

 

Example 14:

Example 15:

Understand special value in R

 


C.  Class Exercises and Solutions: 

a) Class Exercises:

Exercises: (S-2(1, 2)) [60 min]

 

Q1) Perform the following data analysis using R

 

 

 

 

 

 

 

 

 

 

 

 

 

Q2) Perform the following data analysis using R

 

Q3) Perform the following data analysis using R

 

 

b) Solutions to Class Exercises:

For Q1 and Q2 Shown as above

Q3)

 

 

 

Q4) Perform the following data analysis using R

Create the following two vectors:

 

Ø  A vector called weight, storing the values: 71, 67, 83, 67

 

Ø  A vector called height, storing the values: 1.75, 1.81, 1.78, 1.82, 1.97, 2.12, 2.75

 

Ø  Calculate the BMI for all you have data for. BMI is calculated by dividing the weight in kg by the height in meters squared.

 

Ø  Carry out the operation and save the result in a variable called bmi. Print your variable bmi

 

Ø  Can you explain how you got the last three values, given that your weight variable only had 4 elements?

 

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 1)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Problem 2) Run the following data conversion program

 

 

 

b) Solution to homework

 

 

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

 

 

Problem 4) Run the following data conversion program

 

# 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]

Q1) Perform the following data analysis using R

 

Ø 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)]

 

 

Q2) Perform the following data analysis using R

 

Ø  # 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)

 

Q3) Perform the following data analysis using R

 

Q4) Perform the following data analysis using R

Create the following two vectors:

 

Ø  A vector called weight, storing the values: 71, 67, 83, 67

 

Ø  A vector called height, storing the values: 1.75, 1.81, 1.78, 1.82, 1.97, 2.12, 2.75

 

Ø  Calculate the BMI for all you have data for. BMI is calculated by dividing the weight in kg by the height in meters squared.

 

Ø  Carry out the operation and save the result in a variable called bmi. Print your variable bmi

 

Ø  Can you explain how you got the last three values, given that your weight variable only had 4 elements?

 

                                                 

 

 

 

 

 

                D.  Homework Exercises: 

a)   Homework Exercises [K-3(2)] [S-2(2,3)] [60 min]

Problem 1)

Description: Graphical user interface, text, application, email

Description automatically generated

 

Problem 2) Run the following data conversion program

Ø  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)

 

 

Problem 3) Run the following data conversion program

Ø  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 4) Run the following data conversion program

 

# 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

 

Skill

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):

 

Learning More Data Objects in R

1)    Understand and use array in R. Data analysis using array. [App F-K-3(1,2,3)] [App F-S-2(1,2,3)] [30 min]

2)    Learn more about ‘MATRIX’ and its computations in R. [App F-K-3(1,2,3)] [App F-S-2(1,2,3)] [45 min]

3)    Learn to assign (and refer) column or row names of matrix objects. [App F-K-3(3)] [App F-S-2(3)] [45 min]

4)    Learn list object in R, create list object, refer and work on the elements of a list, and manipulate list. [App F-K-3(1,2,3)] [App F-S-2(1,2,3)] [60 min]

5)    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. [App F-K-3(1,2,3)] [App F-S-2(1,2,3)] [60 min]

6)  Classroom exercises [60 min]
Example 1:

 

 

 


 

Example 2:

 

Understand array

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example 3:

 

Create arrays

 


 

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]

 

Q1) Perform the following data analysis using R

 

b) Solutions to Class Exercises:

 

 


 

D.  Homework Exercises and Solutions: 

a)   Homework Exercises [60 min]

Problem 1)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b) Solution to homework for 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"))

 

 

 

 

 

评论

此博客中的热门博文

Don't be afraid, this time the two-way foil is in the hands of civilization别怕,这一次二向箔掌握在文明的手中

Zelensky signed a deal to sell the country泽连斯基签下协议,把国家给卖了,以后乌克兰的事,华尔街说了算

Marianne Situ的帖子