Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove two largest unique numbers from a vector

Tags:

r

I have x as

x <- c("7", "2", "3", "8", "8")

I want output

[1] "2" "3" "8"

and remove one of 8 and 7. Hence removing one of largest two numbers.

like image 993
Vasista B Avatar asked Nov 30 '22 10:11

Vasista B


1 Answers

Here's a possibility with match().

x[-match(tail(sort(unique(x)), 2), x)]
# [1] "2" "3" "8"
like image 192
Rich Scriven Avatar answered Dec 15 '22 11:12

Rich Scriven