Question: Problem is solved using RStudio The command `sample(x,size=n,replace=TRUE)` picks n elements from the vector `x` at random with replacement (the same element can be picked

Problem is solved using RStudio

The command `sample(x,size=n,replace=TRUE)` picks n elements from the vector `x` at random with replacement (the same element can be picked twice). Imagine that over a customer's lifetime they make either 1, 2, ..., or 10 purchases (for all intents and purposes the value can be considered to be picked at random). Each purchase results in the customer spending either 5, 5.5, 6, 6.5, ..., or 20 dollars (for all intents and purposes the value can be considered to be picked at random).

Using a `for` loop, generate the lifetime values (total spent) of 50000 customers and put them in a vector called `lifetimevalue`.

Include a histogram of the values with `hist(lifetimevalue,breaks=seq(from=0,to=max(lifetimevalue)+5,by=5))` (this makes bars of width 5 starting at 0), the output of `summary`, and the overall average lifetime value in your writeup. You'll want to follow best practices for writing `for` loops to get this right!

Hints:

* Get your code working for the first customer before writing the loop!

* First, create a variable called `purchases` which contains the (randomly picked) number of purchases that this customer makes during his or her lifetime (i.e., left-arrow into `purchases` the output of the appropriate `sample` command). purchase <- sample(50, 50000, replace = TRUE)

* Then, create a vector `amounts` by left-arrowing the output of the appropriate `sample` command. This vector will store the amounts of money spent on each of this customers purchases. For instance, if `purchases` happened to equal 6, then `amounts` will be a vector of length 6 containing 6 randomly chosen monetary values. If `purchases` happened to be 2, then this will be a vector of length 2 containing 2 randomly chosen monetary values. The sum of the values in `amounts` gives this customer's lifetime value.

* If you need help setting up the `sample` command to generate `amounts`, go back to the description of `sample`. The first argument is a vector of values to select from (use `seq` to specify this). The second argument is the number of values to pick. This number is "random", but is stored in `purchases`!

* Once you are confident in your code, initialize a vector `lifetimevalue` to be an empty vector, and have a `for` loop fill in its values. You're looping over the integers 1, 2, 3, (The i-th element of `lifetimevalue` will be the lifetime value of customer `i` and will equal the sum of the numbers in the `amounts` vector created).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!