1

I want a function for the mode of a vector. Abhiroop Sarkar's answer to This question works, but I want to understand why.

Here is the code

Mode <- function(x){

y <- data.frame(table(x))
y[y$Freq == max(y$Freq),1]
}

1) Wy do we need to put the table in a data frame,

2) in this line

 y[y$Freq == max(y$Freq),1]

what does the y$Freq do? is frequency a default columns in the table?

Peter_Pan
  • 113
  • 6

1 Answers1

3

When we convert a table output to data.frame, it creates a two column data.frame

set.seed(24)
v1 <- table(sample(1:5, 100, replace  = TRUE))
y <- data.frame(v1)
y
#  Var1 Freq
#1    1   19
#2    2   24
#3    3   22
#4    4   16
#5    5   19

The first column 'Var1' is the names of the frequency output from table and the 'Freq' is the actual frequency of those names

y[y$Freq == max(y$Freq), 1]
#[1] 2
#Levels: 1 2 3 4 5

Now, we are subsetting the first column 'Var1' based on the max value of 'Freq', and it returns a vector because of the drop = TRUE in [ when there is a single column

If we want to return a data.frame with single, add drop = FALSE at the end

y[y$Freq == max(y$Freq), 1, drop = FALSE]
#   Var1
#2    2

Regarding the default name Freq, it is created from the as.data.frame.table method

as.data.frame.table
function (x, row.names = NULL, ..., responseName = "Freq", stringsAsFactors = TRUE, 
    sep = "", base = list(LETTERS)) 
{
    ex <- quote(data.frame(do.call("expand.grid", c(dimnames(provideDimnames(x, 
        sep = sep, base = base)), KEEP.OUT.ATTRS = FALSE, stringsAsFactors = stringsAsFactors)), 
        Freq = c(x), row.names = row.names))
    names(ex)[3L] <- responseName
    eval(ex)
}
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I understand everything in your answer except for the last sentence. Why does it return a vector? (I get that we are looking at the first column in y based on the max value of Freq) – Peter_Pan Jan 18 '20 at 17:29
  • 1
    @Jess If you check `?Extract`, the default usage is `x[i, j, ... , drop = TRUE]` i.e. I I change it to `y[y$Freq == max(y$Freq), 1, drop = FALSE]` it returns a data.frame – akrun Jan 18 '20 at 17:30