ASSIGNMENT 1 - ANSWERS

Question 1:

Suppose you keep track of your mileage each time you fill up. At your last 6 fill-ups the mileage was.

65311 65624 65908 66219 66499 66821 67145 67447

Enter these numbers into R. Use the function diff on the data. What does it give?

Answer 1:

my.mileageVector=c(65311,65624,65908,66219,66499,66821,67145,67447)
diff(my.mileageVector) #Diff function gives differences in the vector
## [1] 313 284 311 280 322 324 302

Question 2:

Suppose you track your commute times for two weeks (10 days) and you find the following times in minutes.

17 16 20 24 22 15 21 15 17 22

Enter this into R. Use the function max to find the longest commute time, the function mean to find the average and the function min to find the minimum.

Oops, the 24 was a mistake. It should have been 18. How can you fix this? Do so, and then find the new average.

How many times was your commute 20 minutes or more? To answer this one can try (if you called your numbers commutes)

What do you get? What percent of your commutes are less than 17 minutes? How can you answer this with R?

Answer 2:

Part 1:
my.commuteTimes=c(17,16,20,24,22,15,21,15,17,22)
max(my.commuteTimes) # Max function gives Maximum in the vector
## [1] 24
mean(my.commuteTimes) # Mean function gives Average of the vector
## [1] 18.9
min(my.commuteTimes) # Min function gives Minimum in the vector
## [1] 15
Part 2:
my.commuteTimes[4]=18 #Accesing Fourth index of the vector and replacing the old one by 18
mean(my.commuteTimes)
## [1] 18.3
Part 3:
sum(my.commuteTimes>=20)
## [1] 4
Part 4:
sum(my.commuteTimes<17)/length(my.commuteTimes)*100 # Calculating the percent of commutes are less than 17 minutes
## [1] 30

Question 3:

Your cell phone bill varies from month to month. Suppose your year has the following monthly amounts

46 33 39 37 46 30 48 32 49 35 30 48

Enter this data into a variable called bill.

  • Use the sum command to find the amount you spent this year on the cell phone.

  • What is the smallest amount you spent in a month?

  • What is the largest?

  • How many months was the amount greater than $40? What percentage was this?

Answer 3:

Bill=c(46,33,39,37,46,30,48,32,49,35,30,48) # Creating a vector named Bill
Part 1:
sum(Bill) # Sum function gives the total of the vector
## [1] 473
Part 2:
min(Bill) # Min function gives Minimum in the vector
## [1] 30
Part 3:
max(Bill) # Max function gives Maximum in the vector
## [1] 49
Part 4:
sum(Bill>40) # Number of months the amount greater than $40
## [1] 5
sum(Bill>40)/length(Bill)*100 # Percentage of months the amount greater than $40
## [1] 41.66667

Question 4:

You want to buy a used car and find that over 3 months of watching the classifieds you see the following prices (suppose the cars are all similar)

9000 9500 9400 9400 10000 9500 10300 10200

Use R to find the average value and compare it to Edmund’s (http://www.edmunds.com) estimate of $9500. Use R to find the minimum value and the maximum value. Which price would you like to pay?

Answer 4:

my.carprices=c(9000,9500,9400,9400,10000,9500,10300,10200)
mean(my.carprices)
## [1] 9662.5
min(my.carprices)
## [1] 9000
max(my.carprices)
## [1] 10300
Average of Edmund’s is less than classifieds.I would prefer to pay Edmund’s prices

Question 5:

Try to guess the results of these R commands and run it in R. Remember, the way to access entries in a vector is with []. Suppose we assume

x = c(1,3,5,7,9) y = c(2,3,5,7,11,13)

Answer 5:

x = c(1,3,5,7,9) # Creating a vector named x
y = c(2,3,5,7,11,13) # Creating a vector named y
  • x+1
x+1 # Adding 1 to vector x
## [1]  2  4  6  8 10
  • y*2
y*2 #Multiplying vector y by 2
## [1]  4  6 10 14 22 26
  • length(x);length(y)
length(x) #Length function gives length of the vector
## [1] 5
length(y)
## [1] 6
  • sum(x>5);sum(x[x>5])
sum(x>5)
## [1] 2
sum(x[x>5])
## [1] 16
  • sum(x>5 | x< 3) read | as “or”, and & as “and”
sum(x>5 | x< 3)  
## [1] 3
  • y[-3]
y[-3]
## [1]  2  3  7 11 13
  • y[x] What is NA?
y[x] 
## [1]  2  5 11 NA NA
Here Vector x acts as a postion for Vector y. It gives the index to the vector y. So in this case there are no elements for postion 7 and 9 in vector y i.e NA
  • y[y>=7]
y[y>=7] 
## [1]  7 11 13

Question 6:

Let the data x be given by

x = c(1, 8, 2, 6, 3, 8, 5, 5, 5, 5)

Use R to compute the following functions. Note, we use X1X1 to denote the first element of x (which is 1) etc.

  • (X1+X2+X3…+X10)/10 use sum

  • Find log10(Xi)log10(Xi) for each i. (Use the log function which by default is base e)

  • Find (X4.4i)/2.875 for each i. (Do it all at once)

  • Find the difference between the largest and smallest values of x. (This is the range. You can use max and min or guess a built in command.)

Answer 6:

x = c(1, 8, 2, 6, 3, 8, 5, 5, 5, 5) # Creating a vector named x
Part 1:
sum(x/10)
## [1] 4.8
Part 2:
log10(x) #Log function gives the log of the vector
##  [1] 0.0000000 0.9030900 0.3010300 0.7781513 0.4771213 0.9030900 0.6989700
##  [8] 0.6989700 0.6989700 0.6989700
Part 3:
(x^4.4)/2.875
##  [1]    0.3478261 3273.0911040    7.3433484  923.0551563   43.7215623
##  [6] 3273.0911040  413.8378128  413.8378128  413.8378128  413.8378128
Part 4:
max(x)-min(x) #Range is calculated by the difference between the largest and smallest values of x
## [1] 7