As we talked about in class, one common way to measure the distance between two numbers $$a$$ and $$b$$ is to compute

$distance = (a-b)^2$

For this code challenge, you need to write a function that

• Takes in two vectors of numbers (the vectors will have the same number of elements in them)

• Computes the distance between corresponding elements in each vector (i.e., the distance between the first element in the the first vector and the first element in the second vector, etc.)

• Returns the average distance between corresponding elements in each vector.

Rules:

• You cannot load any external packages, but any base R functions are allowed

To win

• You must email me your function brantly.callaway@uga.edu

• I’ll call the function on two vectors of numbers. If it computes the correct average distance between two vectors, then you win.

Solution below…

mean_distance <- function(x,y) {
distance <- (x-y)^2
mean(distance)
}

mean_distance(c(10,3,5), c(0.1,3,0))
## [1] 41.00333