Calculating Mean and Mode of week days and Months in R

Hi, i am facing some difficulties with calculating the Mean and Mode of the below Df containing a conversion from date to days of the week and their respective months.
Please could someone help??

For the mean for each day, try

TimeStats <- DF |> group_by(ride_day) |> summarize(Avg = mean(time_diff))

I'm not familiar with the functions to calculate the mode, but the form of the code would be the same.

1 Like

Thanks, i'll give it a try.

find_mode <- function(x) {
  unique_values <- unique(x)
  tab <- tabulate(match(x, unique_values))
  mode_values <- unique_values[tab == max(tab)]

Thank you very much, this was very helpful. I wonder why R is unable to count individual days on the Df column containing the week days.

It seems strange to me that there is no standard function for mode in R
And I would think many people want a single mode - which find_mode doesn't do

Here is my general purpose (most simple classes) attempt at mode with single value returned

# MostCommon is most often used in summarise to get the single most common non-empty value in a vector (ie grouping in group_by)
# where there is a tie, it returns max value in sort order
# it is mode - but only ever returning one value

MostCommon <- function(x) {
  ux <- unique(x)
  uxnotna <- ux[which(!]
  if(length(uxnotna) > 0) {
    tab <- tabulate(match(x, uxnotna))
    candidates <- uxnotna[tab == max(tab)]
    if (is.logical(x)) {
      any(candidates) # return TRUE if any true. max returns an integer
    } else {
      max(candidates) # return highest (ie max) value
  } else {
1 Like

DescTools::Modereturns the most frequent value as number or character, depending of class(x) . If there is more than one, all are returned in a vector.

If John Chambers, a guy who ate lunch with John Tukey, Dennis Ritchie, Ken Thompson and the other cool kids at Bell Labs didn't think that {base} or [stats} needed a mode(), I'm fine with that.

Einstein’s admitted his mistake with his Cosmological Constant. But I suspect it took a greater mind than mine to convince him.
I just end up using my function to return just one value a lot

1 Like

I don't understand what you mean by the above statement. Can you state it in another way or give an example?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.