Example: I have a data frame as below:
| Date | value |
| 1/2/2020 | 1 |
| 1/3/2020 | 2 |
| 1/4/2020 | 3 |
| 1/5/2020 | 1 |
| 1/6/2020 | 1 |
| 1/7/2020 | 3 |
The result should be:
| 1/2/2020 | 1 |
| 1/5/2020 | 1 |
| 1/6/2020 | 1 |
I had tried
df[which.min(df$value),]
but it didn't work
CodePudding user response:
which.min
only returns the index of first minimum value
which.min(c(5, 1, 3, 1, 2))
[1] 2
Instead, create a logical vector with ==
on the min
value
df[df$value == min(df$value, na.rm = TRUE),]
Or use %in%
which would be safe even when there are NA
values (return FALSE where NA are found whereas ==
returns NA
)
df[df$value %in% min(df$value, na.rm = TRUE),]