Nauman Khalid
Nauman Khalid

Reputation: 13

Selecting values from data frame

I Want to select a value from the data frame which is greater than the specified value. my data frame consist of single column only

I'm trying which.max function but that only returns the first value of the data frame

d[which.max(d$slope > 14.9),]

I want a single value. very first one greater than the specified

Upvotes: 0

Views: 104

Answers (1)

Clemsang
Clemsang

Reputation: 5491

You can sort your dataframe by slope then use Position to have the first match:

dd <- d[order(d$slope), , drop = FALSE]
dd$slope[Position(function(x) x > 14.9, dd$slope)]

Position evaluates until the first TRUE condition only. It should be much faster than using which or a full vector comparison.

Or you can use which.min to get the lower value that is greater than your threshold:

d[d$slope > 14.9][which.min(d$slope[d$slope > 14.9]),]

Upvotes: 1

Related Questions