How to choose the most distant values ​​from the average value of the vector

Assume that the vector

vector<-c( 0.096846906,  0.068149926, -0.019938431, -0.095515090, -0.109936195, -0.006755265, -0.207243555,  0.117235435, -0.036333873, -0.156043650, -0.334150484,
   0.141990040, -0.116270635,  0.079373531,  0.070359814,  0.090415147,  0.046807444, -0.024908308,  0.022005548,  0.015559027,  0.065343488,  0.039524657,
   0.077209216,  0.051124695,  0.076794957, -0.059121977,  0.071967601,  0.042357348,  0.039801927,  0.053932640, -0.036346802, -0.070258993, -0.105611663,
   -0.138738161, -0.044395825, -0.194363631, -0.127153662,  0.052912436,  0.163879916,  0.087960810,  0.005298789, -0.191104683,  0.113214756,  0.045232380)

a mean=-0.007702101. I want to choose the values ​​that are most different from the average. Suppose the 10 most distant values ​​are from the mean.

I use this code to get the values ​​that are farthest from the average:

sort(abs(vector-mean(vector)))

So now I have a number of differences that are ordered. But I can’t find a way to get the values ​​(from the data with the name: vector) for which the greatest distance (again, the 10 most distant). I'm sure this is something simple, but I'm really stuck!

+4
source share
3 answers

There are many ways to do this. Based on your example and requirements, you can do

m <- 10
vector[head(order(abs(vector - mean(vector)), decreasing = TRUE), m)]
#[1] -0.3341505 -0.2072436 -0.1943636 -0.1911047  0.1638799  0.1419900 -0.1560437 -0.1387382  0.1172354  0.1132148

:

  • ( ). , ( sort)
  • m, head. [1:m]
  • , vector
+3

dplyr:

vector%>%as_tibble()%>%mutate(dist_Mean = abs(value - mean(value) ))%>%arrange(desc(dist_Mean))%>%top_n(10)
+1

- , .

mn <- mean(vector) std <- sd(vector),

d <- 1.5
vector[vector<(mn-d*std) | vector>(mn+d*std)]

#[1] -0.2072436 -0.3341505 -0.1943636  0.1638799 -0.1911047

d, , .

(d=1 , 1 )

0

Source: https://habr.com/ru/post/1692642/


All Articles