[英]How can i return list of min value in dataframe in r?
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但它没有用
which.min
only returns the index of first minimum value which.min
只返回第一个最小值的索引
which.min(c(5, 1, 3, 1, 2))
[1] 2
Instead, create a logical vector with ==
on the min
value相反,在min
上创建一个带有==
的逻辑向量
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
)或者使用%in%
即使有NA
值也是安全的(在找到 NA 的地方返回 FALSE 而==
返回NA
)
df[df$value %in% min(df$value, na.rm = TRUE),]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.