I am able to calculate the mode using
Mode <- function(x) {
ux <- unique(x)
tab <- tabulate(match(x, ux)); ux[tab == max(tab)]
}
However, my data frame has more NA values in it than integers so when I run this Mode, I get back NA.
Is there a way of removing NA values in the above function somehow or would I need to remove NA values from the dataframe initially? If so, how? I tried
DF.omit.NA<- na.omit(DF) ###and na.exclude
and it produced no rows of data.
We can use na.omit
wrapped around the unique
elements
Mode <- function(x) {
ux <- na.omit(unique(x) )
tab <- tabulate(match(x, ux)); ux[tab == max(tab) ]
}
Mode(v1)
#[1] 1
v1 <- c(1, 3, NA, 2, 1, 1, NA, NA, NA)