Nauman Khalid
Nauman Khalid

Reputation: 13

Selecting the value greater than specified in dataframe

I've to select only one value that is greater than the specified value(let's say 14.92) in a dataframe. My dataframe have only one column.

I'm trying something like this

value <-data[which(data[,X4]>14.9),]

I am expecting a single value that is equal to to greater than specified value i.e 14.9

Upvotes: 0

Views: 1278

Answers (2)

akrun
akrun

Reputation: 887213

We can use slice

library(dplyr)
df %>%
    filter(a > 3) %>% 
    slice(1) %>% 
    pull(a)
#[1] 4

data

df <- data.frame(a = 1:5)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389012

You can use which.max which returns first maximum of the group which would be the first TRUE when compared to a logical vector.

data[which.max(data$colname >14.9),]

Or subset which and select only one value

data[which(data$colname > 14.9)[1], ]

Consider an example,

df <- data.frame(a = 1:5)
value <- df[which.max(df$a > 3), ]
value
#[1] 4

df[which(df$a > 3)[1], ]
#[1] 4

Upvotes: 2

Related Questions