简体   繁体   English

min()为什么不返回实际最小值?

[英]Why does min() not return the actual minimum value?

I'm trying to find the minimum value in a list of numerical values in a table using the function min() in R, but I've noticed that it sometimes doesn't return the actual lowest numerical value. 我正在尝试使用R中的min()函数在表的数值列表中找到最小值,但是我注意到它有时不返回实际的最低数值。

For instance, if the list consisted of 7.760, 12.015, 13.043, and 70.789, if I did min(list), it would return 12.015 as the minimum value and not 7.760. 例如,如果列表由7.760、12.015、13.043和70.789组成,则如果我执行min(list),它将返回12.015作为最小值,而不是7.760。 I noticed by just manually sorting the table of values from lowest to highest using the arrows at the top of the column in the table, it would return the order of 12.015, 13.043, 7.760, 70.789 as if ranking the values by the first digit instead of the number as a whole. 我注意到,通过使用表中列顶部的箭头手动将值的表从最低到最高进行排序,它会返回12.015、13.043、7.760、70.789的顺序,就像按第一位对值进行排序一样整个数量。

Is there a way to fix it or a different function to use in this case? 有没有一种方法可以解决它,或者在这种情况下可以使用其他功能?

Sounds like your numbers are actually character strings 听起来您的数字实际上是字符串

vec <- c(7.760, 12.015, 13.043, 70.789)

min(vec)
# [1] 7.76

vec <- as.character(vec)
min(vec)
# [1] "12.015"

Simply convert them to numeric with as.numeric(vec) , then min() will behave as expected 只需将它们转换为带有as.numeric(vec) numeric ,然后min()将表现出预期的效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在循环中使用 min(i) 来逐列返回分组中的最小值? - How to use min(i) in a loop to return the minimum value in group by column? 如何在循环中使用 which.min(i) 来返回每个变量中每组的最小值? - How to use which.min(i) in a loop to return the minimum value per set in each variable? R:为什么观星者使用此 dataframe 返回不正确的最小值和最大值? - R: why does stargazer return incorrect values for min and max with this dataframe? 为什么我在 ggplot2 中的情节图例没有显示最小值和最大值? 它显示的是连续值,但不是最大值和最小值? - Why my plot's legend in ggplot2, doesn't show minimum and maximum value? It is showing a continuous value but not max and min? 为什么 nrow() 返回 NULL 值? - Why does nrow() return a NULL value? 返回分组数据子组的最小值 - Return min value for subgroups of grouped data 为什么此函数未在R中找到最小值? - Why this function is not finding the minimum value in R? 为什么这个应用函数返回的值与循环不同? - Why does this apply function return a different value than the loop? 当预测值没有变化时,为什么lm会返回值? - Why does lm return values when there is no variance in the predicted value? 为什么 ifelse() 返回的 integer 值不是真或假参数 r - Why does ifelse() return an integer value that is NOT a true or false argument r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM