18.3 Chapter 6: Vector Functions

  1. Create a vector that shows the square root of the integers from 1 to 10.
(1:10) ^ .5
##  [1] 1.0 1.4 1.7 2.0 2.2 2.4 2.6 2.8 3.0 3.2

#or

sqrt(1:10)
##  [1] 1.0 1.4 1.7 2.0 2.2 2.4 2.6 2.8 3.0 3.2
  1. Renata thinks that she finds more treasure when she’s had a mug of grogg than when she doesn’t. To test this, she recorded how much treasure she found over 7 days without drinking any grogg (ie., sober), and then did the same over 7 days while drinking grogg (ie., drunk). Here are her results:
Table 17.1: Renata’s treasure haul when she was sober and when she was drunk
day sober drunk
Monday 2 0
Tuesday 0 0
Wednesday 3 1
Thursday 1 0
Friday 0 1
Saturday 3 2
Sunday 5 2

How much treasure did Renata find on average when she was sober? What about when she was drunk?

sober <- c(2, 0, 3, 1, 0, 3, 5)
drunk <- c(0, 0, 1, 0, 1, 2, 2)

mean(sober)
## [1] 2
mean(drunk)
## [1] 0.86
  1. Using Renata’s data again, create a new vector called difference that shows how much more treasure Renata found when she was drunk and when she was not. What was the mean, median, and standard deviation of the difference?
difference <- sober - drunk

mean(difference)
## [1] 1.1
median(difference)
## [1] 1
sd(difference)
## [1] 1.3
  1. There’s an old parable that goes something like this. A man does some work for a king and needs to be paid. Because the man loves rice (who doesn’t?!), the man offers the king two different ways that he can be paid. You can either pay me 100 kilograms of rice, or, you can pay me as follows: get a chessboard and put one grain of rice in the top left square. Then put 2 grains of rice on the next square, followed by 4 grains on the next, 8 grains on the next…and so on, where the amount of rice doubles on each square, until you get to the last square. When you are finished, give me all the grains of rice that would (in theory), fit on the chessboard. The king, sensing that the man was an idiot for making such a stupid offer, immediately accepts the second option. He summons a chessboard, and begins counting out grains of rice one by one… Assuming that there are 64 squares on a chessboard, calculate how many grains of rice the main will receive. If one grain of rice weights 1/64000 kilograms, how many kilograms of rice did he get? Hint: If you have trouble coming up with the answer, imagine how many grains are on the first, second, third and fourth squares, then try to create the vector that shows the number of grains on each square. Once you come up with that vector, you can easily calculate the final answer with the sum() function.
# First, let's create a vector of the amount of rice on each square:
# It should be 1, 2, 4, 8, ...
rice <- 2 ^ (0:63)

# Here are the first few spaces
head(rice)
## [1]  1  2  4  8 16 32

# The result is just the sum!
rice.total <- sum(rice)
rice.total
## [1] 1.8e+19

# How much does that weigh? Each grain weights 1/6400 kilograms:
rice.kg <- sum(rice) * 1/6400
rice.kg
## [1] 2.9e+15

# That's 2,900,000,000,000,000 kilograms of rice. Let's keep going....
# A kg of rice is 1,300 calories

rice.cal <- rice.kg * 1300
rice.cal
## [1] 3.7e+18

# How many people can that feed for a year?
# A person needs about 2,250 calories a day, or 2,250 * 365 per year

rice.people.year <- rice.cal / (2250 * 365)
rice.people.year
## [1] 4.6e+12

# So, that amount of rice could feed 4,600,000,000,000 for a year
# Assuming that the averge lifespan is 70 years, how many lifespans could this feed?

rice.people.life <- rice.people.year / 70
rice.people.life
## [1] 6.5e+10

# Ok...so it could feed 65,000,000,000 (65 billion) people over their lives

# Conclusion: King done screwed up.