1

I have a vector x <- c(0.0,0.0,56.4,5.4). How can I return the most frequent value in this vector for this case it should be 0.0?

I have tried the solution provided at Finding the most common elements in a vector in R but this returns a table, this is not what I expect exactly.

x[which.max(sort(table(x), decreasing = TRUE))] works for this case but it doesn't work when I have a vector say x <- c(278.0,0.0,27.8,27.8) or x <- c(NA,0.0,27.8,27.8).

I expect the following results.

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

Community
  • 1
  • 1

0 Answers0