x <-c(1,2,3,4)
y = c(5,6,7,8)
length(x)
## [1] 4
length(y)
## [1] 4
x+y
## [1]  6  8 10 12
x^2
## [1]  1  4  9 16
x^2+y^2 
## [1] 26 40 58 80
ls()
## [1] "x" "y"
# lists all active objects
rm(x,y)
# removes indicated objects
ls()
## character(0)
x <- seq(from=2, length = 5, by = 2)
x
## [1]  2  4  6  8 10
?seq
## starting httpd help server ... done
x[2]
## [1] 4
2:4
## [1] 2 3 4
x[2:4]
## [1] 4 6 8
x[-2]
## [1]  2  6  8 10
x[-2:-4]
## [1]  2 10
c(2:4)
## [1] 2 3 4
c(-2:-4)
## [1] -2 -3 -4
x[-c(2:4)]
## [1]  2 10
x[-c(2,3,4)]
## [1]  2 10
x=matrix(data=c(1,2,3,4),nrow=2,ncol=2)
x
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
y=matrix(c(5,6,7,8),2,2,byrow=T)
y
##      [,1] [,2]
## [1,]    5    6
## [2,]    7    8
# sometimes it is useful to name arguments but this is not necessary
# x is organized by columns (column vectors), y by rows
sqrt(x)
##          [,1]     [,2]
## [1,] 1.000000 1.732051
## [2,] 1.414214 2.000000
x=rnorm(5)
x
## [1] -0.4400762 -1.3831318 -2.1190279 -0.1224321 -0.5939876
y=rnorm(5)
y
## [1] -0.4171735 -0.4773599 -1.2076638  1.0623166 -1.7624579
plot(x,y)

cor(x,y)
## [1] 0.495887
set.seed(2023)
x=rnorm(5)
x
## [1] -0.08378436 -0.98294375 -1.87506732 -0.18614466 -0.63348570
set.seed(2023)
y=rnorm(5)
y
## [1] -0.08378436 -0.98294375 -1.87506732 -0.18614466 -0.63348570
plot(x,y)

cor(x,y)
## [1] 1
x=rnorm(50)
y=rnorm(50)
plot(x,y,xlab="Measurements errors machine 1", 
     ylab="Measurements errors machine 2", 
     main="Measurement errors", col="red")

A=matrix(1:16,4,4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
A[2,3]
## [1] 10
A[c(1,3),c(2,4)]
##      [,1] [,2]
## [1,]    5   13
## [2,]    7   15
A[1:3,2:4]
##      [,1] [,2] [,3]
## [1,]    5    9   13
## [2,]    6   10   14
## [3,]    7   11   15
A[1,]
## [1]  1  5  9 13
A[,1]
## [1] 1 2 3 4
# Note that A[1,] and A[,1] are not matrices but vectors
A[,1,drop=FALSE]
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3
## [4,]    4
A[1:2,]
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
A[,1:2]
##      [,1] [,2]
## [1,]    1    5
## [2,]    2    6
## [3,]    3    7
## [4,]    4    8
A[-c(1,3),]
##      [,1] [,2] [,3] [,4]
## [1,]    2    6   10   14
## [2,]    4    8   12   16
#negative sign eliminates indicated entries
dim(A)
## [1] 4 4
# Analyzing Autos
library(readxl)
setwd("C:/Users/lgawarec/Documents/MATH-650/R files")
getwd()
## [1] "C:/Users/lgawarec/Documents/MATH-650/R files"
# The following commands are comments only because when
# compiling a report, there cannot be two different files
# Undo comments for presentation, then redo comments
#Autos <- read_excel("C:/Users/lgawarec/Documents/MATH-650/Autoxlsx.xlsx")
#summary(Autos)
#View(Autoxlsx)

# Replacing "?" with "NA" - Not Available (missing data)
Autos <- read_excel("C:/Users/lgawarec/Documents/MATH-650/Autoxlsx.xlsx", na="?" )
View(Autos)
summary(Autos)
##       mpg          cylinders      displacement     horsepower        weight    
##  Min.   : 9.00   Min.   :3.000   Min.   : 68.0   Min.   : 46.0   Min.   :1613  
##  1st Qu.:17.50   1st Qu.:4.000   1st Qu.:104.0   1st Qu.: 75.0   1st Qu.:2223  
##  Median :23.00   Median :4.000   Median :146.0   Median : 93.5   Median :2800  
##  Mean   :23.52   Mean   :5.458   Mean   :193.5   Mean   :104.5   Mean   :2970  
##  3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:262.0   3rd Qu.:126.0   3rd Qu.:3609  
##  Max.   :46.60   Max.   :8.000   Max.   :455.0   Max.   :230.0   Max.   :5140  
##                                                  NA's   :5                     
##   acceleration        year           origin          name          
##  Min.   : 8.00   Min.   :70.00   Min.   :1.000   Length:397        
##  1st Qu.:13.80   1st Qu.:73.00   1st Qu.:1.000   Class :character  
##  Median :15.50   Median :76.00   Median :1.000   Mode  :character  
##  Mean   :15.56   Mean   :75.99   Mean   :1.574                     
##  3rd Qu.:17.10   3rd Qu.:79.00   3rd Qu.:2.000                     
##  Max.   :24.80   Max.   :82.00   Max.   :3.000                     
## 
plot(Autos$cylinders,Autos$mpg, xlab="Cylinders",ylab="mpg", 
     main="How cylinders affect mpg")

cylinderQ=as.factor(Autos$cylinders)
cylinderQ
##   [1] 8 8 8 8 8 8 8 8 8 8 8 8 8 8 4 6 6 6 4 4 4 4 4 4 6 8 8 8 8 4 4 4 4 6 6 6 6
##  [38] 6 8 8 8 8 8 8 8 6 4 6 6 4 4 4 4 4 4 4 4 4 4 4 4 4 8 8 8 8 8 8 8 8 8 3 8 8
##  [75] 8 8 4 4 4 4 4 4 4 4 4 8 8 8 8 8 8 8 8 8 8 8 8 6 6 6 6 6 4 8 8 8 8 6 4 4 4
## [112] 3 4 6 4 8 8 4 4 4 4 8 4 6 8 6 6 6 6 4 4 4 4 6 6 6 8 8 8 8 8 4 4 4 4 4 4 4
## [149] 4 4 4 4 6 6 6 6 8 8 8 8 6 6 6 6 6 8 8 4 4 6 4 4 4 4 6 4 6 4 4 4 4 4 4 4 4
## [186] 4 4 8 8 8 8 6 6 6 6 4 4 4 4 6 6 6 6 4 4 4 4 4 8 4 6 6 8 8 8 8 4 4 4 4 4 8
## [223] 8 8 8 6 6 6 6 8 8 8 8 4 4 4 4 4 4 4 4 6 4 3 4 4 4 4 4 8 8 8 6 6 6 4 6 6 6
## [260] 6 6 6 8 6 8 8 4 4 4 4 4 4 4 4 5 6 4 6 4 4 6 6 4 6 6 8 8 8 8 8 8 8 8 4 4 4
## [297] 4 5 8 4 8 4 4 4 4 4 6 6 4 4 4 4 4 4 4 4 6 4 4 4 4 4 4 4 4 4 4 5 4 4 4 4 4
## [334] 6 3 4 4 4 4 4 4 6 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6 6 6 6 8 6 6 4 4 4
## [371] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6 6 4 6 4 4 4 4 4 4 4 4
## Levels: 3 4 5 6 8
plot(cylinderQ,Autos$mpg, varwidth=T,xlab="Number of cylinders", 
     ylab="mpg",main="How cylinders affect mpg")

#win.graph()
#plot(Autos$horsepower,Autos$mpg)
#identify(Autos$horsepower,Autos$mpg, Auto$name, tolerance=0.5)

# Using "MASS" and "ISLR2" packages
# install.packages("MASS")
# install.packages("ISLR2")