Page 278 - Data Science class 11
P. 278

Function body—the actual statements of the function which has instructions to solve a specific task based on the
        information provided by the arguments.

        Return statement—used to return any value from the function.
        R does not have a standard in-built function to calculate mode. So we will create a user function to calculate the mode
        of a data set in R. This function takes the vector as input and gives the mode value as an output
        Example

        Enter the following code snippet in the script panel:

             # Create the function.
             getmode <- function(v) {
                uniqv <- unique(v)
                uniqv[which.max(tabulate(match(v, uniqv)))]

             }
             # Create the vector with numbers.
             v <- c(4,1,4,3,1,4,3,4,2,5,5,3,2,3)
             # Calculate the mode using the user function.
             result <- getmode(v)
             print(result)

             # Create the vector with characters.
             charv <- c("o","it","the","it","it")
             # Calculate the mode using the user function.
             result <- getmode(charv)
             print(result)
        When we execute the above code, it produces the following result:






































          276   Touchpad Data Science-XI
   273   274   275   276   277   278   279   280   281   282   283