Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the n most common values in a vector [duplicate]

Tags:

r

count

ranking

I have a vector say

c(1,1,1,1,1,1,2,3,4,5,7,7,5,7,7,7) 

How do I count each element, and then return the e.g. 3 most common elements, i.e. 1, 7, 5?

like image 710
ChairmanMeow Avatar asked Jun 28 '13 22:06

ChairmanMeow


People also ask

How do you find the common element of multiple vectors?

To find the common elements from multiple vectors, you can use the intersect function from the sets base R package. vectors (of the same mode) containing a sequence of items (conceptually) with no duplicate values. Intersect will discard any duplicated values in the arguments, and they apply as.

How do you find the most repeated value in R?

To find the most frequent factor value in an R data frame column, we can use names function with which. max function after creating the table for the particular column. This might be required while doing factorial analysis and we want to know which factor occurs the most.

How do I count the number of observations in R?

Often you may be interested in counting the number of observations by group in R. Fortunately this is easy to do using the count() function from the dplyr library.

How do you find a match between two vectors in R?

R Match – Using match() and %in% to compare vectors We have two options here: The R match () function – returns the indices of common elements. the %in% operator – returns a vector of True / False results which indicates if a value in the first vector was present in the second.


2 Answers

I'm sure this is a duplicate, but the answer is simple:

sort(table(variable),decreasing=TRUE)[1:3] 
like image 125
Thomas Avatar answered Nov 15 '22 23:11

Thomas


I don't know if this is better than the table approach, but if your list is already a factor then its summary method will give you frequency counts:

> summary(as.factor(c(1,1,1,1,1,1,2,3,4,5,7,7,5,7,7,7))) 1 2 3 4 5 7  6 1 1 1 2 5  

And then you can get the top 3 most frequent like so:

> names(sort(summary(as.factor(c(1,1,1,1,1,1,2,3,4,5,7,7,5,7,7,7))), decreasing=T)[1:3]) [1] "1" "7" "5" 
like image 27
qwwqwwq Avatar answered Nov 15 '22 22:11

qwwqwwq