简体   繁体   English

如何在 r 中返回 dataframe 中的最小值列表?

[英]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.

相关问题 如何使R数据框列的列表取决于另一列的值 - How can I make list of R dataframe columns conditional on value of another column 如何在 R 中返回带有 dataframe 的向量? - How can I return a vector with a dataframe inside in R? 如何计算 R 中 dataframe 中一组其他唯一值的最小/最大项? - How can I count the min/max item for for a set of otherwise unique values within a dataframe in R? 如何在 R 过滤器 max( ) min( ) 和条件值中选择数据框? - How to select dataframe in R filter max( ) min( ) and conditional value? 如何在R中的文本中查找和替换数据框中的最小值 - How to find and replace min value in dataframe with text in r 如何在 R 中返回具有相同最小值或最大值的多行? (现在它只选择一个) - how do I return multiple rows with the same min or max value in R ?? (right now it only picks one) 在R中进行汇总时,如何返回数据框中的唯一值列表而不是列表的索引值? - How do you return the list of unique values in dataframe and not the index value of the list when aggregating in R? 如何在数据框中创建映射到R中的列表的列? - How can I create a column in my dataframe that maps to a list in R? 如何将一长串数字 R 输出传输到数据框中? - How can I transfer long list of numeric R output into a dataframe? 如何从 R 列表中的 dataframe 名称中提取数字? - How can I extract numbers from dataframe names in a list in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM