mean(c(1,2,3))
[1] 2
The mean of numbers \(x_1,...,x_n\) is their average:
\[\frac{x_1 + x_2 + \cdots + x_n}{n}\] In R, we can calculate the mean with the mean
function. For example:
mean(c(1,2,3))
[1] 2
my_mean
, that calculates the mean of a vector. The input to your function should be a vector, and the output should be the mean of the values in that vector. You may not use the built-in mean
function in R, but you may use the sum
and length
functions.<- function(x){
my_mean return(sum(x)/length(x))
}
mean
function for several different input vectors.my_mean(1:10)
[1] 5.5
mean(1:10)
[1] 5.5
<- runif(100)
unif_samp my_mean(unif_samp)
[1] 0.4952387
mean(unif_samp)
[1] 0.4952387
A weighted mean is similar to the usual average, but now we add a weight to each value. The observations with greater weights contribute more to the weighted mean.
The weighted mean of \(x_1,...,x_n\) with weights \(w_1,...,w_n\) is
\[\frac{w_1 x_1 + w_2 x_2 + \cdots + w_n x_n}{w_1 + w_2 + \cdots + w_n}\] The usual arithmetic mean is a special case of the weighted mean with \(w_1 = w_2 = \cdots = w_n = 1\).
my_mean
function from Exercise 1 so that it now takes two inputs – a vector of values x
, and a vector of weights w
– and returns the weighted mean of x
with weights w
. Hint: If x
and w
are two vectors of the same length, then x*w
is a vector created by multiplying each entry of x
with the corresponding entry of w
.<- function(x, w){
my_mean return(sum(x*w)/sum(w))
}
my_mean
function from Exercise 3 so that the default weights are all 1.# making the default weights all 1
<- function(x, w = rep(1, length(x))){
my_mean return(sum(x*w)/sum(w))
}
# should be 2
my_mean(x = c(1, 2, 3))
[1] 2
# should be 2
my_mean(x = c(1, 2, 3), w = c(1, 1, 1))
[1] 2
# should be 1.5
my_mean(x = c(1, 2, 3), w = c(1, 1, 0))
[1] 1.5
Note: You need to make w
a vector of the same length as x
! The following code will run, but is wrong:
my_mean(x = c(1, 2, 3), w = 1)
[1] 6