Week 1 Lecture Slides

Week 1 Lecture Slides: Day1

W1 Day1

R introduction

R as a Calculator

  • R is a statistical computing environment.
2+3
[1] 5
9*25/(24-22)^2
[1] 56.25

R as a Language

  • At the core of R is an interpreted computer language.

    • An interpreted language

      • the system processes commands/expressions entered by the user, who types the commands at the command prompt (>), or submits the commands from a file called a script.

      • In a window system, users interact with R through the R console.

      Object1 <- 24
      Object1
      [1] 24

R as a Language Analogy

  • What is R?

  • What is Language? [Watch the video lecture on this topic that is available on Week1 content of this course, titled same, in Bb].

    • What is Language?

    • Comparison: Human vs. Computer/Programming Language

  • R Commands/expressions

    • Object

    • Operator <- [Assignment Operator]

    • Function [with function.name ()]

So, What is R in practical sense?

  • R works through object [Symbols]

  • We create or manipulate existing symbols/objects using functions with operators

Objects/Symbols in R

An exemplar data set:

  • Tabular data of wide-way type with rows and columns

  • Information about different aspects (variables) of entities (e.g., participants) is represented as a table

  • Each informational unit (data-point) is called a Data Element [DE]

  • Data elements are organized in rows and columns

  • Observations are rows [p] of DEs of each entity/participant

  • Variables are columns [q] of DEs of different entities/participants

    #____________________________
    # P#    q1  q2  q3  q4 
    # ___________________________
    # p1    DE1 DE4 DE7 DE10
    # p2    DE2 DE5 DE8 DE11
    # p3    DE3 DE6 DE9 DE12
    #____________________________

Real data

Week 1 Lecture Slides: Day2

Quick Overview of Day 1

  • Understanding R as a computer

  • Grasping R as a programming language, which includes symbols (objects) and rules (functions) for working with those symbols

  • Using “expressions/commands” to interact with R, similar to how we use expressions in normal conversation

Day2

  • According to Searl’s theory of speech act, language usage involves carrying out illocutionary acts (exchanging expressions) with specific intentions, adhering to certain rules

  • The intention when interacting with R:

    • To instruct R to carry out statistical calculations

    • As a consequence, expressions are more or less commands

    • We utilize operators (+, =, etc.) to convey actions specific to commands, akin to verbs in sentences

    • The basic “expression/command” results in crearing an “object”

    • The fundamental operator used for this purpose is the assignment operator (<-)

    • Use the <- operator to create an object (named object.name) from a function/data-points

      • object.name <- DE #read as create an object named, “object.name” from the “DE”

      • my.name <- “Sri”. #read as create an object named, “my.name” from the DE, “Sri”

Types/Levels of Objects in R

#____________________________
# P#    q1  q2  q3  q4 
# ___________________________
# p1    DE1 DE4 DE7 DE10
# p2    DE2 DE5 DE8 DE11
# p3    DE3 DE6 DE9 DE12
#____________________________
  1. OBJECT-Type 1. DE as an Object

    1. 1. OBJECT-Type 1. DE as an Object

    2. [Information about each Object Type in R]

  2. OBJECT-Type 2. Variable (set of DEs) as an Object

    1. 2. OBJECT-Type 2. Variable/vector (set of DEs) as an Object

    2. Creating Object Type 2 in R

  3. OBJECT-Type 3. Table/Matrix of DEs as an Object

    1. [3. OBJECT-Type 3. Table/Matrix of DEs as an Object]

    2. Creating Object Type 3 in R

A visual representation of 3 object types can be seen @#figure2

1. OBJECT-Type 1. DE as an Object

  • #s

    • #1A. NUMERIC [a Continuous #]

      • One’s Height
    • #1B. INTIGER [a Discrete #]

      • # of times one changes a major
  • Logical

    • Truth value of a statement [ TRUE or FALSE]
  • Characters

    • None-#s and Non-logicals

    • “characters”

2. OBJECT-Type 2. Variable/vector (set of DEs) as an Object

  • Vector of #s

    • Vector of Numerical #s (continuous #s)

      • heights of students in a class {5.6, 6.7, 7.1, 5.9}
    • Vector of Integer #s (discrete #s)

      • # of times students in a class changed their major {0L, 1L, 2L, 1L, 3L}

      • Without L, R will represent these #s as Numerical values (1 would mean 1.00)

  • Vector of Logicals (a set of truth values)

    • truth values of students in a class specific to a statement that they are married {TRUE, FALSE, FALSE, TRUE}
  • Vector of Categories: FACTORS

    • Categories are numbered DEs where DEs are labelled

    • another way to think them as labelled (in “character” format) numbers with no real mathematical value to the #s

      • F1. Nominal Factors (where categories do not have order)

        • e.g., blood group of students in a class {“A”, “A”, “AB”, “O”, “O”, “AB”}
      • F2. Ordinal Factors (where categories have order)

        • e.g., academic year of students in a class {“Freshmen”, “Junior”, “Junior”, “Senior”, “Sophomore”}

3. OBJECT-Type 3. Table of DEs as an Object

  • 3A. A simple Table: MATRIX

    • a table of DEs of same type + The rows/obervations and columns/variables are NOT named

    • Matrices can be denoted by pXq notation, where p-refers to # of rows and q-refers to # of columns).

    • So, a 2X3 Matrix wouuld have 2 rows of DEs arranged in 3 Columns while a 3X2 Matrix would have 3 rows of DEs arranged in 2 columns.

    • Remember, all the DEs in a matrix are of the same type

  • 3B. A special Matrix/Vector: LIST

    • Could be a Vector of DEs of different types, including the ones beyond basic types

      • e.g., vector of DEs of different types {height, weight, gender, data.frame1}

    • Could be a table of objects of different types

      • e.g., a matrix with names for columns and rows

      • Usually, you call into a List function when you take a matrix and add names for rows and columns)

  • 3C. Complex but Most Common Matrix. DATA FRAME

    • Data Frames are Matrices with variable/vector names

    • Eventhough the DEs in a given vector are a ll of same type,

      • A data frame can have vectors/columns/variables of different data types)

  • 3D. ARRAY (a matrix of more than 2 dimensions: rowsXcolumnsXlayers)

    • a matrix has 2-dimenss (rows and columns ) and represented as (pXq)

    • An array in R is a matrix with more than 2 dimensions (rows, columns, and layers) and represented as (pXqXl)

    • Matrices and arrays are tables of DEs of same type without names for rows, columns, and layers

    • # Another way to think about Objects as belonging to 2 types (vectors and tables). In this classification, Objects are starting at the second level (Object-Type 2). Another way to view DEs as Objects with not specific attributes. Vectors and Tables then would be Objects with attributes.

  • To sum up, here is a visual representation of Objects of different types:

Week 2 Lecture Slides

Quick Review of Week 1

  • R is a calculator

  • R is a computer/programming language like a human language

  • R is a language with symbols [objects] and rules to combine objects [functions], which makes them expressions/commands

  • Our interactions with R with the intent to instruct it using expressions/commands

  • Objects are how we interact with R

  • There are three levels of objects we can create in R

    1. DEs as Objects

      • Numeric

      • Integer

      • Logical

      • “Character/string”

    2. Variables as Objects

      • # Vectors

      • Logical Vectors

      • Nominal Factors

      • Ordinal Factors

    3. Tables as Objects

      • Matrix

      • List

      • Data Frame

      • Array

Week2 Day1 Lecture Slides

Creating Objects in R

  1. OBJECT-Type 1. DE as an Object

          #1A. NUMERIC [a Conrinuous #]
                DE.Height.Numeric <- 5.9 #creating a DE-object using an assignment operator
                DE.Age.Numeric <- 26 #creating a DE-object using an assignment operator
              DE.Height.Numeric #printing the object
    [1] 5.9
              print (DE.Age.Numeric) #printing the object using print function
    [1] 26
            #1B. INTIGER [a discrete #]
                DE.Age.Intiger <- 26L #creating a DE-object using an assignment operator
                  DE.Age.Intiger #printing the object
    [1] 26
                  print(DE.Age.Intiger) #printing the object using print function
    [1] 26
            #1C. LOGICAL [a Truth value of T/F]
                DE.Married.Logical <- FALSE #creating a DE-object using an assignment operator
                  DE.Married.Logical #printing the object
    [1] FALSE
                  print(DE.Married.Logical) #printing the object using print function
    [1] FALSE
            #1D. CHARACTER [a character/category with/without meaning and no #]
                DE.Name.Character <- "Sri" #creating a DE-object using an assignment operator
                  DE.Name.Character #printing the object
    [1] "Sri"
                  print(DE.Name.Character) #printing the object
    [1] "Sri"
  2. OBJECT-Type 2. Variable (set of DEs) as an Object

    2A. Vector [ a set of #s or logicals]

    2A1. Numbered Vector (a set of #s)

            #2A1. Numbered Vector (a set of #s)
                vector.ClassHeight.Numbers <- c(5.9, 5.7, 4.8, 5.6) #creating vectors using combine function
                  vector.ClassHeight.Numbers #print the elements of the vector
    [1] 5.9 5.7 4.8 5.6
                  print(vector.ClassHeight.Numbers) #print the vector elements using print function
    [1] 5.9 5.7 4.8 5.6
                  vector.ClassAge.Intigers <- c(26L, 36L, 21L, 22L) #creating the integer-vector by using L
                  vector.ClassAge.Intigers #print the elements of the vector
    [1] 26 36 21 22
                  print(vector.ClassAge.Intigers) #print the vector elements using print function
    [1] 26 36 21 22

    2A2. Logical Vector (a set of logicals)

    #2A2. Logical Vector (a set of Logicals)
                vector.ClassMarried.Logical <- c(TRUE, FALSE, TRUE, FALSE) #creating the logical-v
                vector.ClassMarried.Logical #printing the logical vector
    [1]  TRUE FALSE  TRUE FALSE
                print(vector.ClassMarried.Logical) #print the vector elements using print function
    [1]  TRUE FALSE  TRUE FALSE

    2B. Factor [a set of categories]/Categorical Vector

    #2B1. Nominal Factor (a set of Categories with NO ORDER)

    #2B1. Nominal Factor (a set of Categoriess with NO ORDER)
    
    #Creating nominal factor from "character/string vector)
    NominalFactor1.ClassBloodGroup <- c("A", "B", "O", "AB") #this will result in character vector without labels
                NominalFactor1.ClassBloodGroup #print it to see the elements of the vector
    [1] "A"  "B"  "O"  "AB"
                class(NominalFactor1.ClassBloodGroup) #you can check the vector type by using the class function
    [1] "character"
                Nomfactor.bgldgrp <- factor(NominalFactor1.ClassBloodGroup)
                Nomfactor.bgldgrp
    [1] A  B  O  AB
    Levels: A AB B O
                levels(Nomfactor.bgldgrp) #For factor variables, you can check the levels of the factor by using the levels function
    [1] "A"  "AB" "B"  "O" 
    • Creating nominal factors from a numeric vector
    #Another way to create Nominal factor is by creading a numerical vector and adding levels and labels to it
                NominalFactor1.ClassBloodGroup1 <- c(1, 2, 0, 3)#first enter the data as numbers by giving numbers to the categories
                NominalFactor1.ClassBloodGroup1 #printing it would give a numeric vector, its the first step
    [1] 1 2 0 3
                class(NominalFactor1.ClassBloodGroup1) #check the vector type
    [1] "numeric"
                BloodGroup.asFACTOR <- factor(NominalFactor1.ClassBloodGroup1, levels = c(0, 1, 2, 3), labels = c("O", "A", "B", "AB")) #using the numeric vector to create a factor using factor function
                BloodGroup.asFACTOR #print the new factor
    [1] A  B  O  AB
    Levels: O A B AB
                class(BloodGroup.asFACTOR) #check the vector type using class function
    [1] "factor"
                #this is one way (using factor ()) of creating a factor (nominal)
    • Creating nominal factors from scratch using gl(function)
    #another way of creating nominal factor is by using the gl(function)
                #you'll create this from scratch, lets say you want to create a factor with 20 cases (DEs) half of which need to be labelled "female" and half "male"
                gender.factor <- gl(2,10, labels = c("female", "male")) #gl(# of levels, # of cases in each level, labels=c("level1-label", level2-lable"))
                gender.factor #prints the factor
     [1] female female female female female female female female female female
    [11] male   male   male   male   male   male   male   male   male   male  
    Levels: female male
                class(gender.factor) #check the object type by using the class function
    [1] "factor"
                levels(gender.factor) #this gives the levels of the factor. you can also rename the levels, for which you should know the order in which they are set, for which you need to look at the printed levels, which shows females and males are the levels in that order. I want to change it from female and male to females and males, u will use the same levels function as follows
    [1] "female" "male"  
    levels(gender.factor) <- c("females", "males")
    levels(gender.factor)
    [1] "females" "males"  

    creating nominal factor using a complete factor (function)

    haircolor
    haircolor1 <- factor(haircolor, levels = c("p", "b", "r", "w"), labels = c("pink", "black", "red", "white"))
    
    haircolor1
    print(haircolor1)
    
  3. 2B2. Ordinal Factor ((an ORDERED set of Categories)

            #2B2. Ordinal Factor (an ORDERED set of Categories)
    
    OrdinalFactor.ClassYear <- c("Freshmen", "Sophomore", "Junior", "Senior")
    OrdinalFactor.ClassYear
    [1] "Freshmen"  "Sophomore" "Junior"    "Senior"   
    x <- factor(OrdinalFactor.ClassYear)
    x
    [1] Freshmen  Sophomore Junior    Senior   
    Levels: Freshmen Junior Senior Sophomore
    levels(x)
    [1] "Freshmen"  "Junior"    "Senior"    "Sophomore"
    x <- factor(OrdinalFactor.ClassYear, levels = c("Freshmen", "Sophomore", "Junior", "Senior"))
    x
    [1] Freshmen  Sophomore Junior    Senior   
    Levels: Freshmen Sophomore Junior Senior
    levels(x)
    [1] "Freshmen"  "Sophomore" "Junior"    "Senior"   

    Task: Identify the type of DE-Object you can create from each of the DEs listed in the table below:

  4. OBJECT-Type 3. Table/Matrix of DEs as an Object

    3A. Simple MATRICES (a matrix/table of DEs of same type + The rows/obervations and columns/variables are NOT named.

    1. Matrices can be denoted by pXq notation, where p-refers to # of rows and q-refers to # of columns).

    2. So, a 2X3 Matrix wouuld have 2 rows of DEs arranged in 3 Columns while a 3X2 Matrix would have 3 rows of DEs arranged in 2 columns.

3B. Special Matrix/Vector. LIST

  • (is a matrix/Vector of DEs of different types, including the ones beyond basic types

  • Usually, you call into a List function when you take a matrix and add names for rows and columns)

3C. Complex but Most Common Matrix. DATA FRAME

  • Data Frames are Matrices with variable/vector names and eventhough the DEs in a given vector are a ll of same type,

  • A data frame can have vectors/columns/variables of different data types)

3D. ARRAY

  • a matrix of more than 2 dimensions of rows and columns

Creating Objects of different Types in R

  1. OBJECT-Type 1. DE as an Object

  2. OBJECT-Type 2. Variable (set of DEs) as an Object

  3. OBJECT-Type 3. Table/Matrix of DEs as an Object