JerryTheForester
JerryTheForester

Reputation: 476

How to calculate a mean value from multiple maximal values

I have a variable e.g. c(0, 8, 7, 15, 85, 12, 46, 12, 10, 15, 15)

how can I calculate a mean value out of random maximal values in R?

for example, I would like to calculate a mean value with three maximal values?

Upvotes: 0

Views: 91

Answers (2)

rmuc8
rmuc8

Reputation: 2989

  • First step: You draw a sample of 3 from your data and store it in x
  • Second step: You calculate the mean of the sample

try

dat <- c(0,8,7,15, 85, 12, 46, 12, 10, 15,15)

x <- sample(dat,3)
x
mean(x)

possible output:

> x <- sample(dat,3)
> x
[1] 85 15  0
> mean(x)
[1] 33.33333

Upvotes: 2

user1981275
user1981275

Reputation: 13370

If you mean the three highest values, just sort your vector and subset:

> mean(sort(c(0,8,7,15, 85, 12, 46, 12, 10, 15,15), decreasing=T)[1:3])
[1] 48.66667

Upvotes: 1

Related Questions