Built with Tutify.io

    AIStudyLabs

    Calculate Statistical mode in R

    Calculate Statistical mode in R

    How to get mode in R? If you are new to R Programming you may not find the mode function in R as this is not present as part of the base library. In this article, I will explain how to create a mode() function, its syntax, and return type, and use it with an example.

    <a id="h-1-mode-in-r-introduction"></a>

    1. Mode in R Introduction

    In statistical terminology, the mode is the value that has the highest number of occurrences in a data set. So, the mode can be get from either numeric or character vectors.

    Since R doesn't have a mode base function, we need to create one. This function takes the vector as input and gives the mode value as an output.

    For a character Vector, it returns a character and for a Numeric vector, it returns a numeric value.

    2. Syntax of mode()

    The following is the syntax of the mode() function that returns the mode value from the vector x.

    
    mode(x, na.rm = FALSE)
    

    2.1 Parameters

    2.2 Return Value

    3 Create mode() Function

    Let's create a mode() function similar to the R median() and mean() Functions. It takes similar arguments as input. One difference is mode() takes both character or numeric vector as input.

    
    # Create mode() function to calculate mode
    mode <- function(x, na.rm = FALSE) {
      
      if(na.rm){ #if na.rm is TRUE, remove NA values from input x
        x = x[!is.na(x)]
      }
    
      val <- unique(x)
      return(val[which.max(tabulate(match(x, val)))])
    }
    

    4. Using mode() Function in R DataFrame

    Let's create the DataFrame and use the mode() function on a column.

    
    # Create Data Frame
    df=data.frame(id=c(11,22,33,44,55),
                  name=c("spark","python","R","jsp","R"),
                  price=c(144,NA,144,567,567))
    df
    
    # Usage of mode() Function
    mode(df$id)
    mode(df$name)
    mode(df$price)
    

    Yields below output.

    5. Using mode() with R Vector

    Similarly, let's also get the mode from the values of Vector in T. The following examples demonstrate getting the mode when on numeric and character vectors and also when you have NA values.

    
    # mode() from Vector
    v1 <- c(1,22,3,5,3,1)
    mode(v1)
    
    v2 <- c("AA","BB","CC","AA","CC","CC")
    mode(v2)
    
    v3 <- c(144,NA,144,567,567)
    mode(v3)
    

    Yields below output.

    6. Conclusion

    In this article, you have learned that R doesn't have a mode() function and learned how to create one with examples. And the function we created takes numeric and character function and returns a mode value.

    You can find the complete example at GitHub Project.

    Related Articles