In a vector in R, if I have a data likes:
1 1 2 1 1 3 3 1 1
In this case, 1
is majority.
Of course, I can loop through a list and count manually, but is there a better way in R to find what is the major value in a vector?
In a vector in R, if I have a data likes:
1 1 2 1 1 3 3 1 1
In this case, 1
is majority.
Of course, I can loop through a list and count manually, but is there a better way in R to find what is the major value in a vector?
You can use table
x <- c(1,1,2,1,1,3,3,1,1)
which.max(table(x))
# 1
# 1
Maybe, something like this would be more helpful.
names(which.max(table(x)))
# "1"
Another option is using count
function from plyr
package
library(plyr)
df <- count(x)
df[which.max(df$freq),1]
# [1] 1
We could try
as.numeric(names(which.max(table(x))))
Example:
x <- c(5,5,2,5,5,3,3,5,5)
#> as.numeric(names(which.max(table(x))))
#[1] 5
Explanation:
The result of which.max(table(x))
is a named integer, which in this case is
#> which.max(table(x))
#5
#3
Here the value is 3 and the name is "5". This output provides the information that the most frequent entry is the third unique number contained in the vector, counted in ascending order (here the ordered numbers are: 2, 3, and 5) and it has the "name" 5 . In the case of a vector consisting of numbers this naming of a number might look strange, but the vector could as well contain words, like "red", "blue", "green", and then knowing the name of the most frequent word would definitely be useful.
We are only interested in the name, which we can extract with the function names()
. The result is a character, which in our case can be coerced into an integer using as.numeric()
.
We could speed up the table
function using Rcpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
std::map<double, int> tableC(NumericVector x) {
std::map<double, int> counts;
int n = x.size();
for (int i = 0; i < n; i++) {
counts[x[i]]++;
}
return counts;
}
Then, we source
the file,
library(Rcpp)
sourceCpp('Table.cpp')
x <- c(1,1,2,1,1,3,3,1,1)
tableC(x)
#1 2 3
#6 1 2
which.max(tableC(x))
#1
#1