2.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? The difference between elements (2nd - 1st, 3rd - 2nd, etc.)
Note: even though the instructions use x as a variable I opted to name it “difference” to avoid confusion with the x variable used for 2.5.
miles = c(65311, 65624, 65908, 66219, 66499, 66821, 67145, 67447)
x = diff(miles)
You should see the number of miles between fill-ups. Use the max to find the maximum number of miles between fill-ups, the mean function to find the average number of miles and the min to get the minimum number of miles.
max(x)
## [1] 324
mean(miles)
## [1] 66371.75
min(miles)
## [1] 65311
2.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.
commute <- c(17, 16, 20, 24, 22, 15, 21, 15, 17, 22)
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.
max(commute)
## [1] 24
mean(commute)
## [1] 18.9
min(commute)
## [1] 15
Oops, the 24 was a mistake. It should have been 18. How can you fix this? Do so, and then find the new average.
commute[4] <- 18
mean(commute)
## [1] 18.3
How many times was your commute 20 minutes or more? To answer this one can try (if you called your numbers commutes)
sum(commute>=20)
## [1] 4
What do you get? 4
What percent of your commutes are less than 17 minutes? 30. How can you answer this with R?
sum(commute<17)/length(commute)*100
## [1] 30
2.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.
bill <- c(46, 33, 39, 37, 46, 30, 48, 32, 49, 35, 30, 48)
Use the sum command to find the amount you spent this year on the cell phone.
sum(bill)
## [1] 473
What is the smallest amount you spent in a month? What is the largest?
min(bill)
## [1] 30
max(bill)
## [1] 49
How many months was the amount greater than $40? What percentage was this?
sum(bill>40)
## [1] 5
sum(bill>40)/length(bill)*100
## [1] 41.66667
2.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.
car <- c(9000, 9500, 9400, 9400, 10000, 9500, 10300, 10200)
mean(car)
## [1] 9662.5
mean(car) <= 9500
## [1] FALSE
Use R to find the minimum value and the maximum value. Which price would you like to pay?
min(car)
## [1] 9000
max(car)
## [1] 10300
I’d like to pay $9000.
2.5 Try to guess the results of these R commands. 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)
Note: These guesses are what I posted in the discussion forum.
x+1
## [1] 2 4 6 8 10
y*2
## [1] 4 6 10 14 22 26
length(x) + length(y)
## [1] 11
x+y
## Warning in x + y: longer object length is not a multiple of shorter object
## length
## [1] 3 6 10 14 20 14
sum(x>5)
## [1] 2
sum(x[x>5])
## [1] 16
Note: I did 2.5 before I did any of the other problems in this set, which ask for similar queries. Response: I did not expect this. I figure sum(x>5) = 2 because it’s essentially asking to count how many elements fulfill the condition (two: 7 and 9). Is sum(x[x>5]) = 16 because it’s asking to sum all x elements for when x > 5?
sum(x>5 | x< 3)
## [1] 3
y[3]
## [1] 5
y[-3]
## [1] 2 3 7 11 13
y[x]
## [1] 2 5 11 NA NA
y[y>=7]
## [1] 7 11 13
2.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 X1 to denote the first element of x (which is 0) etc. Note: is the “0” a typo if x’s first element is 1?
sum(x)/10
## [1] 4.8
log10(x[1:10])
## [1] 0.0000000 0.9030900 0.3010300 0.7781513 0.4771213 0.9030900 0.6989700
## [8] 0.6989700 0.6989700 0.6989700
(x[1:10]-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
max(x) - min(x)
## [1] 7