Exercise

# Weighted average (3)

Let's look at an example of recycling. What if you wanted to give equal weight to your Microsoft and Sony stock returns? That is, you want to be invested 50% in Microsoft and 50% in Sony.

```
ret <- c(7, 9)
weight <- .5
ret_X_weight <- ret * weight
ret_X_weight
[1] 3.5 4.5
```

`ret`

is a vector of length 2, and `weight`

is a vector of length 1. R reuses the `.5`

in `weight`

twice to make it the same length of `ret`

, then performs the element-wise arithmetic.

Instructions

**100 XP**

- A named vector,
`ret`

, containing the returns of 3 stocks is in your workspace. - Print
`ret`

to see the returns of your 3 stocks. - Assign the value of
`1/3`

to`weight`

. This will be the weight that each stock receives. - Create
`ret_X_weight`

by multiplying`ret`

and`weight`

. See how R recycles`weight`

? `sum()`

the`ret_X_weight`

variable to create your equally weighted`portf_ret`

.- Run the last line of code multiplying a vector of length 3 by a vector of length 2. R reuses the 1st value of the vector of length 2, but notice the warning!