MATH 4720/MSSC 5720 Introduction to Statistics

5 <= 5[1] TRUE
5 <= 4[1] FALSE
# Is 5 NOT equal to 5? FALSE
5 != 5[1] FALSE
A variable stores a value that can be changed according to our need.
Use <- operator to assign a value to the variable. (Highly recommended๐)
x <- 5 ## we create an object, value 5, and call it x, which is a variable.
x ## type the variable name to see the value stored in the object x[1] 5
(x <- x + 6) # We can reassign any value to the variable we created[1] 11
x == 5 # We can perform any operations on variables[1] FALSE
log(x) # Variables can also be used in any built-in functions[1] 2.397895
Use typeof() to check which type a variable belongs to.
Common types include character, double, integer and logical.
Check if itโs of a specific type: is.character(), is.double(), is.integer(), is.logical().
Type character and logical correspond to categorical variables.
Type logical is a special type of categorical variables that has only two categories (binary).
double and integer correspond to numerical variables. (an exception later)
double is for continuous variablesinteger is for discrete variables.Create a variable age that stores your age. Check what type it is.
Create a variable name that stores your name. Check its type.
Create a variable is_male that stores whether you are male (true/false). Check its type.
02:00
To create a vector, use c(), short for concatenate or combine.
All elements of a vector must be of the same type.
To extract element(s) in a vector, use a pair of brackets [] with element indexing.
The indexing starts with 1.
factor can be ordered in a meaningful way. Create a factor by factor().[1] med high low
Levels: high low med
dim.matrix() to create a matrix., to separate row and column index.mat[2, 2] extracts the element of the second row and second column.mat [,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
## all rows and 2nd column
## leave row index blank
## specify 2 in coln index
mat[, 2][1] 4 5 6
## 2nd row and all columns
mat[2, ] [1] 2 5
## The 1st and 3rd rows and the 1st column
mat[c(1, 3), 1] [1] 1 3
A data frame is of type list of equal-length vectors, having a 2-dimensional structure.
More general than matrix: Different columns can have different types.
Use data.frame() that takes named vectors as input โelementโ.
## data frame w/ an dbl column named age and char columns gen and col.
(df <- data.frame(age = c(19, 21, 40), gen = c("m", "f", "m"), col = c("r","b","g"))) age gen col
1 19 m r
2 21 f b
3 40 m g
str(df) ## use $ to represent column elements 'data.frame': 3 obs. of 3 variables:
$ age: num 19 21 40
$ gen: chr "m" "f" "m"
$ col: chr "r" "b" "g"
What happen if we create a data frame without column names?

Data frame has properties of matrix.
x that has 5 elements 3, 6, 2, 9, 14.x.mtcars data set by selecting variables mpg and disp.mtcars that have 4 cylinders.05:00