yopy
yopy

Reputation:

Find n smallest values from data?

How to get 3 minimum value on the data automatically?

Data:

data <- c(4,3,5,2,2,1,1,5,6,7,8,9)
[1] 4 3 5 2 2 1 1 5 6 7 8 9

With min() function just return 1 value and I want to get 3 minimum value from data.

min(data)
[1] 1

Can I have this from a data?

[1] 1 1 2

Upvotes: 8

Views: 20630

Answers (2)

Tim
Tim

Reputation: 7464

Simply take the first three values of a sorted vector

> sort(data)[1:3]
[1] 1 1 2

Another alternative is head function that shows you the first n values of R object, so for three highest numbers you need head of a sorted vector

> head(sort(data), 3)
[1] 1 1 2

...but you could take head of possibly any other R object.

If you were interested in value that marks the upper boundry of k percent lowest values, use quantile function

> quantile(data, 0.1)
10% 
1.1 

Upvotes: 12

Sycorax
Sycorax

Reputation: 1386

data <- c(4,3,5,2,2,1,1,5,6,7,8,9)
sort(data,decreasing=F)[1:3]

Upvotes: 2

Related Questions