Homework 1
#2.1
miles = c(65311, 65624, 65908, 66219, 66499, 66821, 67145, 67447)
x = diff(miles)
x
## [1] 313 284 311 280 322 324 302
y = max(x)
y
## [1] 324
z = mean(x)
z
## [1] 305.1429
w = min(x)
w
## [1] 280
#2.2
commute = c(17, 16, 20, 24, 22, 15, 21, 15, 17, 22)
p = max(commute)
p
## [1] 24
q = mean(commute)
q
## [1] 18.9
r = min(commute)
r
## [1] 15
commute[4] = 18
commute
## [1] 17 16 20 18 22 15 21 15 17 22
s = mean(commute)
s
## [1] 18.3
sum( commute >= 20)
## [1] 4
sum( commute < 17)
## [1] 3
sum( commute < 17) / length (commute)
## [1] 0.3
#2.3
bill = c(46, 33, 39, 37, 46, 30, 48, 32, 49, 35, 30, 48)
sum(bill)
## [1] 473
min(bill)
## [1] 30
max(bill)
## [1] 49
sum( bill > 40)
## [1] 5
sum( bill > 40) / length (bill)
## [1] 0.4166667
#2.4
usedcar = c(9000, 9500, 9400, 9400, 10000, 9500, 10300, 10200)
mean(usedcar)
## [1] 9662.5
median(usedcar)
## [1] 9500
#Edmund's price of 9500 matches the median and is lower than the mean.
min(usedcar)
## [1] 9000
max(usedcar)
## [1] 10300
#Of course, the price of 9000 sounds the best to me.
#2.5 Expected
#x = c(1,3,5,7,9)
#y = c(2,3,5,7,11,13)
#2.5.1
#x+1
#1 will be added to each of the 5 values in x
#2.5.2
#y*2
#Each value in y will be multiplied by 2
#2.5.3
#length(x) and length(y)
#It will tell us the number of items in x and the number of items in y
#2.5.4
#x + y
#It will show the sum of the 1st items in x and y and then the 2nd and so on but will get confused on the 6th item in y
#2.5.5
#sum(x>5) and sum(x[x>5])
#Will show number of numbers in x greater than 5 and then the sum or those numbers
#2.5.6
#sum(x>5 | x< 3)
#Will show number of numbers in x greater than 5 or less than 3
#2.5.7
#y[3]
#The 3rd value from the left in y
#2.5.8
#y[-3]
#The 3rd value from the right in y
#2.5.9
#y[x]
#This is not possible
#2.5.10
#y[y>=7]
#There are 2 values greater than 7 so it will show the 2nd value in y
#2.6
x = c(1, 8, 2, 6, 3, 8, 5, 5, 5, 5)
#2.6.1
sum(x)/10
## [1] 4.8
#2.6.2
log(x,10)
## [1] 0.0000000 0.9030900 0.3010300 0.7781513 0.4771213 0.9030900 0.6989700
## [8] 0.6989700 0.6989700 0.6989700
#2.6.3
(x-4.4)/2.875
## [1] -1.1826087 1.2521739 -0.8347826 0.5565217 -0.4869565 1.2521739
## [7] 0.2086957 0.2086957 0.2086957 0.2086957
#2.6.4
diff(range(x))
## [1] 7