R Select Most Frequent Values from Vector or Column (Example Code)
In this article, I’ll illustrate how to select the most frequent elements of a vector or data frame variable in R programming.
Example Data
x <- c(1, 4, 3, 5, 7, 6, 1, 1, 4, 3, 4) # Example data x # Show data in RStudio console # [1] 1 4 3 5 7 6 1 1 4 3 4 |
x <- c(1, 4, 3, 5, 7, 6, 1, 1, 4, 3, 4) # Example data x # Show data in RStudio console # [1] 1 4 3 5 7 6 1 1 4 3 4
Example: Select Most Common Data Points from Vector Object
sort(table(x), decreasing = TRUE)[1:3] # Show most common values # x # 1 4 3 # 3 3 2 |
sort(table(x), decreasing = TRUE)[1:3] # Show most common values # x # 1 4 3 # 3 3 2
Related Tutorials
You may find some related R programming tutorials in the list below.