rdesctools

Mode using DescTools in R


I am using R and I've learned to calculate the Mode using CodeAcademy's curriculum. Basically CodeAcademy recommended using DescTools (which is an R Package that I've installed on my computer). The Mode should return the most commonly occurring value in a numerical vector. However, when I run this simple block of code my console in R isn't returning anything. It's just reprinting the line of code I've executed.

Is something wrong with my IDE or have I made a mistake in writing this block of code? I've read so many threads and used the ?Mode help function, but it seems that I've written the code properly from what I can tell. I'm just not getting any result to show up in my console when executing the code.

install.packages("DescTools")
require(DescTools)
?Mode
my_data <- c(15,8,9,15,12,13,2,15,13,8,13,6,7)
Mode2 <- Mode(my_data)
print(Mode2)
Mode2
view(Mode2)

As you can see in the code block above, I tried to use print() to view the data, I tried to just type Mode2 to render it to the workbook, and I tried using view(), but none of these lines result in any output in my console.


Solution

  • If we want to get the Mode, another option is to create one using base R options

    Mode <- function(x) {
      ux <- unique(x)
      ux[which.max(tabulate(match(x, ux)))]
    }
    

    -testing

    > Mode(my_data)
    [1] 15