1. Vector

# Create a vector
c(1, 1.3, 8)
## [1] 1.0 1.3 8.0
# Create a sequence of numbers from 2 to 5
x = 2:5

# Print the vector
x
## [1] 2 3 4 5
# Square the elements of the vector and add 1/4
x^2 + 1/4
## [1]  4.25  9.25 16.25 25.25
# Get the class of the vector
class(x)
## [1] "integer"
# Set the seed for reproducibility
set.seed(2021)

# Generate a random sample of 50 numbers from 1 to 50
y = sample(1:50)

# Print the sample
y
##  [1]  7 38 46 39 12  6 49 44  5 47 23 48 18  3 26 22 31 19  4 21 35 42  9 45 43
## [26] 11 36 27 30 40 15  2 24 16 20  1  8 17 34 29 32 10 41 37 50 33 13 25 28 14
# Summarize the sample
summary(y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00   13.25   25.50   25.50   37.75   50.00
# Get the class of the sample
class(y)
## [1] "integer"
# Get the structure of the sample
str(y)
##  int [1:50] 7 38 46 39 12 6 49 44 5 47 ...
# Head of the cars dataset
head(cars, 8)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
## 7    10   18
## 8    10   26
# Get the class of the cars dataset
class(cars)
## [1] "data.frame"
# Summarize the cars dataset
summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
# Summarize the speed column of the cars dataset
summary(cars$speed)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     4.0    12.0    15.0    15.4    19.0    25.0
# Summarize the dist column of the cars dataset
summary(cars$dist)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2.00   26.00   36.00   42.98   56.00  120.00
# Extract the speed column of the cars dataset
speed = cars$speed

# Calculate the mean of the speed column
mean(speed)
## [1] 15.4
# Calculate the median of the speed column
median(speed)
## [1] 15
# Calculate the minimum of the speed column
min(speed)
## [1] 4
# Calculate the maximum of the speed column
max(speed)
## [1] 25
# Calculate the quantiles of the speed column
quantile(speed)
##   0%  25%  50%  75% 100% 
##    4   12   15   19   25
# Calculate the quantiles of the speed column with probability intervals of 0.2
quantile(speed, probs=seq(0,1, by = 0.20))
##   0%  20%  40%  60%  80% 100% 
##    4   11   14   17   20   25
# Calculate the standard deviation of the speed column
sd(speed)
## [1] 5.287644
# Calculate the variance of the speed column
var(speed)
## [1] 27.95918
# Extract the dist column of the cars dataset
dist = cars$dist

# Calculate the correlation between the speed and dist columns
cor(speed, dist)
## [1] 0.8068949
# Get the structure of the cars dataset
str(cars)
## 'data.frame':    50 obs. of  2 variables:
##  $ speed: num  4 4 7 7 8 9 10 10 10 11 ...
##  $ dist : num  2 10 4 22 16 10 18 26 34 17 ...
# Get the dimensions of the cars dataset
dim(cars)
## [1] 50  2
# View the cars dataset
View(cars)


# Get the first row of the cars dataset
cars[1,]
##   speed dist
## 1     4    2
# Get the first column of the cars dataset
cars[,1]
##  [1]  4  4  7  7  8  9 10 10 10 11 11 12 12 12 12 13 13 13 13 14 14 14 14 15 15
## [26] 15 16 16 17 17 17 18 18 18 18 19 19 19 20 20 20 20 20 22 23 24 24 24 24 25
# Get the first two columns of the cars dataset
cars[,1:2]
##    speed dist
## 1      4    2
## 2      4   10
## 3      7    4
## 4      7   22
## 5      8   16
## 6      9   10
## 7     10   18
## 8     10   26
## 9     10   34
## 10    11   17
## 11    11   28
## 12    12   14
## 13    12   20
## 14    12   24
## 15    12   28
## 16    13   26
## 17    13   34
## 18    13   34
## 19    13   46
## 20    14   26
## 21    14   36
## 22    14   60
## 23    14   80
## 24    15   20
## 25    15   26
## 26    15   54
## 27    16   32
## 28    16   40
## 29    17   32
## 30    17   40
## 31    17   50
## 32    18   42
## 33    18   56
## 34    18   76
## 35    18   84
## 36    19   36
## 37    19   46
## 38    19   68
## 39    20   32
## 40    20   48
## 41    20   52
## 42    20   56
## 43    20   64
## 44    22   66
## 45    23   54
## 46    24   70
## 47    24   92
## 48    24   93
## 49    24  120
## 50    25   85
# Get the first three elements of the dist column of the cars dataset
cars$dist[1:3]
## [1]  2 10  4
# Get the second element of the second column of the cars dataset
cars[c(2,8,4), 2]
## [1] 10 26 22
# Get all rows of the cars dataset except for rows 2 to 49
cars[-c(2:49),]
##    speed dist
## 1      4    2
## 50    25   85
# Get the index of the row with the maximum speed in the cars dataset
which.max(cars$speed)
## [1] 50
# Get the value of the dist column in the row with the maximum speed in the cars dataset
cars[which.max(cars$speed),"dist"]
## [1] 85
# Get the elements of the dist column of the cars dataset that are equal to 10 or 20
cars$dist %in% c(10,20)
##  [1] FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [13]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
## [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [49] FALSE FALSE
# Get all rows of the cars dataset where the value of the dist column is equal to 10 or 20
cars[cars$dist %in% c(10,20),]
##    speed dist
## 2      4   10
## 6      9   10
## 13    12   20
## 24    15   20
# Define a function to calculate a function of x and y
f = function(x,y){
  r = sqrt(x^2 + y^2)
  return(10*sin(r)/r)
}

# Calculate the value of the function for x = 3 and y = 4
f(3,4)
## [1] -1.917849
# Create a sequence of numbers from -10 to 10
x = seq(-10, 10, length=30)

# Create a sequence of numbers from -10 to 10
y = x

# Calculate the function for all values of x and y in the sequences
z=outer(x,y,f)

# Create a 3D plot of the function
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

x=seq(-10,10,length=300)
plot(x,sin(x))

#dummy comment to clear plot

6. Packages

New methods and tools are stored in packages which may need to be installed. ISLR is a commonly used package.

Method 1: use code to download and install package:

install.packages(“ISLR”) #downloads package
library(ISLR) #loads package

Some packages produce warning messages because they are intended for a different version of R. You can ignore the warning messages.

Method 2: In RStudio’s lower right pane, there is a Packages tab. The package you need may appear in the list under this tab. If it does, just check the box and the package will be installed. ISLR should listed under this tab.

Method 3: RStudio may prompt with an offer to install a needed missing package. If this happens, agree to the install.

Hint: If you install a package using code, you will want to comment out that line of code following the install:

#install.packages(“ISLR”)
library(ISLR) <- The load statement can remain