# The Weighted Mean & Median , Screen 6

In the exercise code :

rooms <- houses\$TotRms AbvGrd
rooms <- as.numeric(stringr::str_replace(rooms, ‘10 or more’, ‘10’))
rooms_sorted <- sort(rooms)

# Find the median

middle_indices <- c(length(rooms_sorted) / 2,
** (length(rooms_sorted) / 2) + 1**
** ) # 2930 is even so we need two indices.**
middle_values <- rooms_sorted[middle_indices]
median <- mean(middle_values)

If length(vector) is odd, middle indices will be a float . Float indices is not acceptable.
Also, I didn’t understand : middle_values <- rooms_sorted[middle_indices]

Hey there!

Totally understand the confusion here — happy to help clear things up a bit on my end The purpose of this exercise is to show how the median can be calculated manually if there are an even number if elements in the vector — there’s absolutely more efficient ways of calculating the median which we go over in later missions.

middle_values <- rooms_sorted[middle_indices] indexes the original vector by the two indices that you calculated in the 2 lines prior.

Hope that helps!

1 Like

If length(vector) is odd, we only need the middle index which can be computed as follows avoiding floating index