Package Loading

library("tidyverse")
library("htmltools")
library("bsplus")
library("rmarkdown")
library("gridExtra")
  1. Create two vectors named v1 and v2, where v1 is the sequence of integers from 2 to 6, and v2 is the sequence of integers from 5 to 9.
  2. What is v2 minus v1?
  3. c.What is the inner product of v1 and v2?
  4. Replace the elements in v1+v2 that are greater than 10 with the number 0. Show that vector.
  1. Create a 5 by 5 matrix with the numbers 1 to 25 as its elements, and call it m1.
  2. What is m1 times v1?
  3. What is v1 times m1?
  4. d.What is m1 times the transpose of m1?
  1. Create a data frame with at least five rows and three columns. The first variable (column) should be dates, the second variable should be strings (characters), and the third variable should be numbers. Name each variable something appropriate and short.
  2. Use str() to show that your data frame is appropriately structured.
  3. Save it as a csv file, and then reload the data from the csv file.
  4. Create a new data frame that is just a subset of your data: the first, third, and last rows, and the first two variables.
  5. Replace all the even numbers in the original data frame with 0.
  6. Create a list with v1, v2, m1, and your data frame. Give all the items in that list a name. Now pick out the third item’s second item.
  1. Using latex equation notation in your .Rmd file, write out the quadratic formula, so that in your html file it looks pretty and like the version we all learned in high school. (Eg, see the box in the top right of this wikipedia page: Quadratic Equation

1

1.1 Create two vectors

named v1 and v2, where v1 is the sequence of integers from 2 to 6, and v2 is the sequence of integers from 5 to 9.

v1 <- c(2:6)
v2 <- c(5:9)

1.2 What is v2 minus v1?

v2-v1
## [1] 3 3 3 3 3

1.3 What is the inner product of v1 and v2?

v2*v1
## [1] 10 18 28 40 54

1.4 Replace the elements

in v1+v2 that are greater than 10 with the number 0. Show that vector.

v3 <- replace(v1+v2, v1+v2 > 10, 0)
v3
## [1] 7 9 0 0 0

2

2.1 Create a 5 by 5 matrix

with the numbers 1 to 25 as its elements, and call it m1.

m1 <- matrix(data = c(1:25),nrow=5,ncol=5)
m1
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    6   11   16   21
## [2,]    2    7   12   17   22
## [3,]    3    8   13   18   23
## [4,]    4    9   14   19   24
## [5,]    5   10   15   20   25

2.2 What is m1 times v1?

m1*v1
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2   12   22   32   42
## [2,]    6   21   36   51   66
## [3,]   12   32   52   72   92
## [4,]   20   45   70   95  120
## [5,]   30   60   90  120  150

2.3 What is v1 times m1?

v1*m1
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2   12   22   32   42
## [2,]    6   21   36   51   66
## [3,]   12   32   52   72   92
## [4,]   20   45   70   95  120
## [5,]   30   60   90  120  150

2.4 What is m1 times the transpose of m1?

m1*t(m1)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1   12   33   64  105
## [2,]   12   49   96  153  220
## [3,]   33   96  169  252  345
## [4,]   64  153  252  361  480
## [5,]  105  220  345  480  625

3

3.1 Create a data frame

with at least five rows and three columns. The first variable (column) should be dates, the second variable should be strings (characters), and the third variable should be numbers. Name each variable something appropriate and short.

dates <- c("10/24/1990",
"9/10/1985",
"12/31/1990",
"6/29/1988",
"8/03/1991")
Dates <- as.Date(dates, "%m/%d/%Y")
names <- c("LiCF",
"StHo",
"CaHo",
"WiHo",
"ChHo")
lpn <- c(8L,
2L,
8L,
9L,
3L)
lifepaths <- data.frame(Dates,names,lpn,stringsAsFactors = FALSE)
colnames(lifepaths) <- c("Birthdays", "Names", "Life_Path_Numbers")
lifepaths
##    Birthdays Names Life_Path_Numbers
## 1 1990-10-24  LiCF                 8
## 2 1985-09-10  StHo                 2
## 3 1990-12-31  CaHo                 8
## 4 1988-06-29  WiHo                 9
## 5 1991-08-03  ChHo                 3

3.2 Use str()

to show that your data frame is appropriately structured.

str(lifepaths)
## 'data.frame':    5 obs. of  3 variables:
##  $ Birthdays        : Date, format: "1990-10-24" "1985-09-10" ...
##  $ Names            : chr  "LiCF" "StHo" "CaHo" "WiHo" ...
##  $ Life_Path_Numbers: int  8 2 8 9 3

3.3 Save it as a csv file,

and then reload the data from the csv file.

write.csv(lifepaths, file = "LifePaths.csv",row.names=F, na="")
csv <- read.csv(file="LifePaths.csv")
csv
##    Birthdays Names Life_Path_Numbers
## 1 1990-10-24  LiCF                 8
## 2 1985-09-10  StHo                 2
## 3 1990-12-31  CaHo                 8
## 4 1988-06-29  WiHo                 9
## 5 1991-08-03  ChHo                 3

3.4 Create a new data frame

that is just a subset of your data: the first, third, and last rows, and the first two variables.

df135 <- data.frame(lifepaths[c(T,F),-3],stringsAsFactors = F)
df135
##    Birthdays Names
## 1 1990-10-24  LiCF
## 3 1990-12-31  CaHo
## 5 1991-08-03  ChHo

3.5 Replace all the even numbers

in the original data frame with 0.

lifepaths[,3] <- replace(lifepaths[,3],lifepaths[,3]%%2==0,0)
lifepaths
##    Birthdays Names Life_Path_Numbers
## 1 1990-10-24  LiCF                 0
## 2 1985-09-10  StHo                 0
## 3 1990-12-31  CaHo                 0
## 4 1988-06-29  WiHo                 9
## 5 1991-08-03  ChHo                 3

3.6 Create a list

with v1, v2, m1, and your data frame. Give all the items in that list a name. Now pick out the third item’s second item

hwk1 <- list(v1,v2,m1,lifepaths)
names(hwk1) <- c("V1","V2","M1","Lifepaths")
hwk1[[3]][,2]
## [1]  6  7  8  9 10

4

4.1 Using latex equation notation

in your .Rmd file, write out the quadratic formula, so that in your html file it looks pretty and like the version we all learned in high school. (Eg, see the box in the top right of this wikipedia page: Quadratic Equation

\(x=\frac{-b\pm \sqrt{b^2-4ac}}{2a}\)