# 1. Store the results of the following operations in the specified variable.
# vector x contains the value 3
x <- 3
# vector y stores the value 5
y <- 5
# vector z is the result of x divided by y
z <- x/y
# vector a has the value Applied Statistics
a <- 'Applied Statistics'
# vector q stores the result of: 8 * 2 ^ 2
q <- 8*2^2
# vector ‘message‘ stores “Hello World!”
'message' <- "Hello World"
# 2. Create a numeric vector (1) using the c() function that has the following values: 4, -10, 33, 0, -121.
'numeric vector' <- c(4, -10, 33, 0, -121)
`numeric vector`
## [1] 4 -10 33 0 -121
# Then (2) add the value 8 to the original vector.
c(`numeric vector`, 8)
## [1] 4 -10 33 0 -121 8
reverse <- seq(100:75)
# 3. Create a vector using (:) operator, named reverse, that starts at 100 and ends at 75,consecutively.
reverse <- seq(100, 75, -1)
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
# 4. Create a vector, named ccny that stores the characters C, C, N, Y
# I keep getting error message:
# Error: object 'N' not found
# 5. Let’s perform some basic arithmetic using vectors.
# a. First, we will make two numeric vectors using the c() function:
# the first vector, v1, will have the values 1, 2, 3
v1 <- c(1, 2, 3)
v1
## [1] 1 2 3
# The second vector, v2, will have the values 5, 6, 7
v2 <- c(5, 6, 7)
v2
## [1] 5 6 7
# b. Add vectors v1 and v2
v1+v2
## [1] 6 8 10
#c. subtract vectors v1 and v2
v1-v2
## [1] -4 -4 -4
#d. multiply vectors v1 and v2
v1*v2
## [1] 5 12 21
# 6. Repeat the vector fives which stores: 1, 2, 3, 4, 5 ten times vectorwise
vector_fives <- c(1, 2, 3, 4, 5)
rep(vector_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
# 7. Repeat the vector eLement which stores consecutive number from 10 to 15 (inclusively) ten times component-wise.
vector_eLement <- seq(10, 15, 1)
vector_eLement
## [1] 10 11 12 13 14 15
rep(vector_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
# 8. Create a sequence of values from 0 to 100, stepping up 20.
seq(0, 100, 20)
## [1] 0 20 40 60 80 100
# 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
#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
#factor(marital_status)
#marital_status <- factor(marital_status, order=TRUE,levels= c(never married, married,divorced,and widowed))
#marital_status
#ERROR MESSAGE I GET
#label: unnamed-chunk-10
#Quitting from lines 119-137 (VectorLabHW.Rmd)
#Error in parse(text = x, srcfile = src) : <text>:13:69: unexpected symbol
#12:
#13: marital_status <- factor(marital_status, order=TRUE,levels= c(never married
# ^
#Calls: <Anonymous> ... <Anonymous> -> parse_all -> parse_all.character -> parse
#Execution halted
#11. Coffee shops usually sell their drinks in one of three (3) sizes: small, medium, or large. A coffee shot 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’
#Sizes <- c(small, medium, large)
#small <- 's'
#medium <- 'm'
#large <- 'l'
#small
#medium
#large
#factor(Sizes, levels= c(small, medium, large))
#Sizes <- factor(Sizes, order=TRUE,levels= c(small, medium, large))
#Sizes
#ERROR MESSAGE I GET
#label: unnamed-chunk-11
#
#Quitting from lines 148-167 (VectorLabHW.Rmd)
#Error in eval(expr, envir, enclos) : object 'small' not found
#Calls: <Anonymous> ... withVisible -> eval_with_user_handlers -> eval -> eval
#Execution halted