我有一个向量x <- c(0.0,0.0,56.4,5.4)
。在这种情况下,如何返回此向量中最频繁的值,该值应该为0.0
?
我已经尝试过Finding the most common elements in a vector in R提供的解决方案,但这会返回一个表,这并不是我所期望的。
x[which.max(sort(table(x), decreasing = TRUE))]
适用于这种情况,但是当我说矢量x <- c(278.0,0.0,27.8,27.8)
或x <- c(NA,0.0,27.8,27.8)
时不起作用。
我希望得到以下结果。
x <- c(278.0,0.0,27.8,27.8)
[1] I expect 27.8, not 278
x <- c(NA,0.0,27.8,27.8)
[1] I expect 27.8, not NA