showkey
showkey

Reputation: 358

how to delete one max and one min values from a vector?

I want to delete one max and one min values from a vector.

> x<-c( 1,1,1,3,8,9,9)

I want to get 1,1,3,8,9 as my result.

> y<-c(max(x),min(x))
> y

[1] 9 1

setdiff(x,y)
[1] 3 8

setdiff can't work . How can i get it?

Upvotes: 0

Views: 3034

Answers (3)

Ben Bolker
Ben Bolker

Reputation: 226182

Another possibility:

x[-c(which.min(x),which.max(x))]

(which.min() and which.max() identify the first occurrence of the min or max value respectively)

Upvotes: 5

Simon O&#39;Hanlon
Simon O&#39;Hanlon

Reputation: 59970

There's about a bazillion ways....

#  Assuming your data is already sorted as in OP,
#  here's a relatively inefficient way to do it...
head(tail(x,-1),-1)
#[1] 1 1 3 8 9

Upvotes: 3

Thomas
Thomas

Reputation: 44525

> x[order(x)][2:(length(x)-1)]
[1] 1 1 3 8 9

Upvotes: 2

Related Questions