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