1

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.

Phil
  • 7,287
  • 3
  • 36
  • 66
ClaireLandis
  • 325
  • 3
  • 18
  • What do you get as output? – akrun Oct 03 '21 at 18:55
  • Well that is part of what is strange. No output. The console just re-prints that executed line of code in blue. Doesn't return a value at all, as if it's not calculating anything? No error, no output. – ClaireLandis Oct 03 '21 at 18:56
  • Do you get any error like this `DescTools::Mode(my_data) Error in fastModeX(x, narm = FALSE) : function 'Rcpp_precious_remove' not provided by package 'Rcpp'` – akrun Oct 03 '21 at 18:58
  • I got that question the first time that I ran the code, but then I deleted the code and started over from scratch. Then at that point, the error didn't occur again, but neither did any other output. – ClaireLandis Oct 03 '21 at 19:01
  • 1
    I also had the same issue, if you do this in R console, it just terminate the session after the first warning. Probably, because the Rcpp required by DescTools is not uptodate – akrun Oct 03 '21 at 19:02
  • It is always good to check the behavior in R console because the IDE can mask the behavior and if it is in shiny, it is another layer and we would never know what exactly is going on – akrun Oct 03 '21 at 19:18

1 Answers1

0

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
akrun
  • 874,273
  • 37
  • 540
  • 662