Vector Lab Homework KEY

Question 1.

# vector x contains the value 3.
x <- 3
x
## [1] 3
# vector y stores the value 5
y <- 5
y
## [1] 5
# vector z is the result of x divided by y
z <- x/y
z
## [1] 0.6
# vector a has the value Applied Statistics
a <- "Applied Statistics"
a
## [1] "Applied Statistics"
# vector q stores the result of: 8 * 2 ^ 2
q <- 8 * 2 ^ 2
q
## [1] 32
# vector ‘message‘ stores “Hello World!”
message <- "Hello World"
message
## [1] "Hello World"

Question 2: Create a numeric vector (1) using the c() function that has the following values: 4, -10, 33, 0, -121. Then (2) add the value 8 to the original vector.

number <- c(4, -10, 33, 0, -121)
number
## [1]    4  -10   33    0 -121
number <- c(number, 8)
number
## [1]    4  -10   33    0 -121    8

Question 3. Create a vector using (:) operator, named reverse, that starts at 100 and ends at 75, consecutively.

reverse <- 100:75
reverse
##  [1] 100  99  98  97  96  95  94  93  92  91  90  89  88  87  86  85  84  83  82
## [20]  81  80  79  78  77  76  75

Question 4. Create a vector, named ccny that stores the characters C, C, N, Y

ccny <- c("C", "C", "N", "Y")
ccny
## [1] "C" "C" "N" "Y"

Question 5. Let’s perform some basic arithmetic using vectors.

  1. First, we will make two numeric vectors using the c() function:
    • the first vector, v1, will have the values 1, 2, 3.
    • The second vector, v2, will have the values 5, 6, 7.
v1 <- c(1,2,3)
v1
## [1] 1 2 3
v2 <- c(5,6,7)
v2
## [1] 5 6 7
  1. Add vectors v1 and v2
v1 + v2
## [1]  6  8 10
  1. subtract vectors v1 and v2
v1 - v2
## [1] -4 -4 -4
  1. multiply vectors v1 and v2
v1 * v2
## [1]  5 12 21

Question 6. Repeat the vector fives which stores: 1, 2, 3, 4, 5 ten times vectorwise.

fives <- c(1,2,3,4,5)
fives
## [1] 1 2 3 4 5
rep(fives, 10)
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3
## [39] 4 5 1 2 3 4 5 1 2 3 4 5

Question 7. Repeat the vector eLement which stores consecutive number from 10 to 15 (inclusively) ten times component-wise.

eLement <- 10:15
eLement
## [1] 10 11 12 13 14 15
rep(eLement, each=10)
##  [1] 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 12 12 12 12 12
## [26] 12 12 12 12 12 13 13 13 13 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14 14
## [51] 15 15 15 15 15 15 15 15 15 15

Question 8. Create a sequence of values from 0 to 100, stepping up 20.

seq(0,100,20)
## [1]   0  20  40  60  80 100

Question 9. Create a sequence of values from 75 to 20, stepping down 5.

seq(75, 20, -5)
##  [1] 75 70 65 60 55 50 45 40 35 30 25 20

Question 10. Let’s define our variable marital_status as having four (4) levels: never married, married, divorced, and widowed. Create a factor has these components: 0, 2, 1, 0, 1, 3, 0, 1, 1, 2, 1, 0, 3 with clear indication of any factor parameter. Suppose that:

• “never married” is 0
• “married” is 1
• “divorced” is 2
• “widowed” is 3

marital_status <- c(0, 2, 1, 0, 1, 3, 0, 1, 1, 2, 1, 0, 3)
marital_status
##  [1] 0 2 1 0 1 3 0 1 1 2 1 0 3
marital_status_factor <- factor(marital_status, labels= c("never-married", "married", "divorced", "widowed"))
marital_status_factor
##  [1] never-married divorced      married       never-married married      
##  [6] widowed       never-married married       married       divorced     
## [11] married       never-married widowed      
## Levels: never-married married divorced widowed

Question 11. Coffee shops usually sell their drinks in one of three (3) sizes: small, medium, or large. A coffee shop in the neighborhood noticed that it served a total of 15 drinks in total, recorded as: s, m, m, s, l, s, l, l, m, s, s, l, m, l, l. Create a factor has these components with clear indication of any necessary factor parameters . Suppose that:

• “small” is ‘s’
• “medium” is ‘m’
• “large” is ‘l’

drinks <- c("s", "m", "m", "s", "l", "s", "l", "l", "m", "s", "s", "l", "m", "l", "l")
drinks
##  [1] "s" "m" "m" "s" "l" "s" "l" "l" "m" "s" "s" "l" "m" "l" "l"
drinks_factor <- factor(drinks, levels = c("s","m","l"), labels = c("small", "medium","large"), order=T)
drinks_factor
##  [1] small  medium medium small  large  small  large  large  medium small 
## [11] small  large  medium large  large 
## Levels: small < medium < large

Data-Frame In Class Practice.

Question 1.

# generate vector for each column
Guest <- c("Jennifer", "David", "Jack", "Joanna", "Victoria") 
Age <- c(19,20,21,25,23)
Friend_Of <- c(1,2,1,2,2)

# check the length
length(Guest)
## [1] 5
length(Age)
## [1] 5
length(Friend_Of)
## [1] 5
# data-frame
wedding <- data.frame(Guest, Age, Friend_Of)
wedding
##      Guest Age Friend_Of
## 1 Jennifer  19         1
## 2    David  20         2
## 3     Jack  21         1
## 4   Joanna  25         2
## 5 Victoria  23         2

Question 2.

typeof(wedding$Age)
## [1] "double"

Question 3.

typeof(wedding$Friend_Of)
## [1] "double"

Question 4.

wedding$Friend_Of <- factor(wedding$Friend_Of, labels = c("Groom", "Bride"))
wedding
##      Guest Age Friend_Of
## 1 Jennifer  19     Groom
## 2    David  20     Bride
## 3     Jack  21     Groom
## 4   Joanna  25     Bride
## 5 Victoria  23     Bride

Question 5.

typeof(wedding$Friend_Of)
## [1] "integer"

Question 6.

They are different because we convert the original column into a factored column (integer data-type).

Question 7.

str(wedding)
## 'data.frame':    5 obs. of  3 variables:
##  $ Guest    : chr  "Jennifer" "David" "Jack" "Joanna" ...
##  $ Age      : num  19 20 21 25 23
##  $ Friend_Of: Factor w/ 2 levels "Groom","Bride": 1 2 1 2 2