TWest
TWest

Reputation: 824

How can I calculate the mean of the top 4 observations in my column?

How can I calculate the mean of the top 4 observations in my column?

c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)

For instance, in the above I would have (50+60+50+60)/4 = 55. I only know how to use the quantile, but it does not work for this.

Any ideas?

Upvotes: 1

Views: 441

Answers (6)

Kyle
Kyle

Reputation: 11

You could use the order function. Order by -x to give the values in descending order, and just average the first 4:

x <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)

mean(x[order(-x)][1:4])
[1] 55

Upvotes: 1

Roland
Roland

Reputation: 132706

Just to show that you can use quantile in this exercise:

mean(quantile(x,1-(0:3)/length(x),type=1))
#[1] 55

However, the other answers are clearly more efficient.

Upvotes: 1

Arun
Arun

Reputation: 118799

Since you're interested in only the top 4 items, you can use partial sort instead of full sort. If your vector is huge, you might save quite some time:

x <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)
idx <- seq(length(x)-3, length(x))
mean(sort(x, partial=idx)[idx])
# [1] 55

Upvotes: 6

Justin
Justin

Reputation: 43255

To be different! Also, please try to do some research on your own before posting.

x <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)    
mean(tail(sort(x), 4))

Upvotes: 1

Maxim.K
Maxim.K

Reputation: 4180

Maybe something like this:

v <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)
mean(head(sort(v,decreasing=T),4))

First, you sort your vector so that the largest values are in the beginning. Then with head you take the 4 first values in that vector, subsequently taking the mean value of that.

Upvotes: 1

user1981275
user1981275

Reputation: 13370

Try this:

vec <- c(12, 13, 15, 1, 5, 9, 34, 50, 60, 50, 60, 4, 6, 8, 12)

mean(sort(vec, decreasing=TRUE)[1:4])

gives

[1] 55

Upvotes: 2

Related Questions