# Trimmed mean in a specific column?

One of the problems in my assignment for class is the following:

# Calculate a so-called "trimmed mean" of column 9 of x_mat by trimming off 5 percent of the values from each end of x. Please do not store the result in an object.

I have to be making this harder than it actually is. Here is what I came up with:

mean(x_mat[, 9], trim = 0.1)

From the help file on the mean function (emphasis mine)

`trim` the fraction (0 to 0.5) of observations to be trimmed from each end of `x` before the mean is computed. Values of trim outside that range are taken as the nearest endpoint.

I am not too sure how to interpret this info into a coding format in R.. can this be explained differently?

The question you presented asks that you "[trim] off 5 percent of values from each end of x". The help file says that the trim argument is " the fraction (0 to 0.5) of observations to be trimmed from each end of `x`". How much will be trimmed off each end of x_mat[,9] by the code you presented:

``````mean(x_mat[, 9], trim = 0.1)
``````

I am being somewhat indirect to try to fulfill the spirit of the forum's homework policy.

Ah fair enough. Thanks for the input. It is difficult knowing if the coding is actually doing what you expected when R gives you a result. Rather it just say the input was bad because then at least you know it is wrong!

Let us create a simple example to get familiar with the function. Simple vector with 10 elements.

``````x<-c(2,3,1,1,1,1,1,1,3,2)
sum(x)
#>  16
mean(x)
#>  1.6
``````

Created on 2020-09-26 by the reprex package (v0.3.0)

Then we go ahead and remove 1 element from each end. So we have element 2 to 9 (after ordering). 1 and 3 are removed. We are trimming 1/10=0.1 from each end.

``````x<-c(2,3,1,1,1,1,1,1,3,2)

x[order(x)][2:9]
#>  1 1 1 1 1 2 2 3

sum(x[order(x)][2:9])/8
#>  1.5

mean(x,trim=0.1)
#>  1.5
``````

Created on 2020-09-26 by the reprex package (v0.3.0)

Hope this helps.

That makes total sense. I just need to do this for a data set of 1000. But I understand the steps and more importantly the logic.
Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.