2+3[1] 5
9*25/(24-22)^2[1] 56.25
2+3[1] 5
9*25/(24-22)^2[1] 56.25
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
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 ()]
R works through object [Symbols]
We create or manipulate existing symbols/objects using functions with operators
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
#____________________________
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
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”
#____________________________
# P# q1 q2 q3 q4
# ___________________________
# p1 DE1 DE4 DE7 DE10
# p2 DE2 DE5 DE8 DE11
# p3 DE3 DE6 DE9 DE12
#____________________________OBJECT-Type 1. DE as an Object
[Information about each Object Type in R]
OBJECT-Type 2. Variable (set of DEs) as an Object
Creating Object Type 2 in R
OBJECT-Type 3. Table/Matrix of DEs as an Object
[3. OBJECT-Type 3. Table/Matrix of DEs as an Object]
Creating Object Type 3 in R
A visual representation of 3 object types can be seen @#figure2
#s
#1A. NUMERIC [a Continuous #]
#1B. INTIGER [a Discrete #]
Logical
Characters
None-#s and Non-logicals
“characters”

Vector of #s
Vector of Numerical #s (continuous #s)
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)
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)
F2. Ordinal Factors (where categories have order)
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:
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
DEs as Objects
Numeric
Integer
Logical
“Character/string”
Variables as Objects
# Vectors
Logical Vectors
Nominal Factors
Ordinal Factors
Tables as Objects
Matrix
List
Data Frame
Array
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"
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"
#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)#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)
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"

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.
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.
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
OBJECT-Type 1. DE as an Object
OBJECT-Type 2. Variable (set of DEs) as an Object
OBJECT-Type 3. Table/Matrix of DEs as an Object