Home > front end >  Problem using na.rm=TRUE in summarize in R code
Problem using na.rm=TRUE in summarize in R code

Time:11-06

Could you help me understand why if I do like this: summarize(Mode=Test1(time, na.rm = TRUE),.groups = 'drop') in the code below, it doesn't work? I just inserted na.rm=TRUE

library(dplyr)

Test <- structure(
  list(date = c("2021-11-01","2021-11-02","2021-11-10"),
       Week= c("Wednesday","Wednesday","Thursday"),
       time = c(5,4,0)),
  class = "data.frame", row.names = c(NA, -3L))

Test1 <- function(t) {
  s <- table(as.vector(t))
  names(s)[s == max(s)]}

Test2<-Test%>%
  group_by(Week = tools::toTitleCase(Week)) %>% 
  summarize(Mode=Test1(time),.groups = 'drop')

> Test2
  Week      Mode 
1 Thursday   0    
2 Wednesday  4    
3 Wednesday  5 

CodePudding user response:

If we want to find the mode, use Mode

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

and now it should work

Test%>%
   group_by(Week = tools::toTitleCase(Week)) %>% 
   summarize(Mode=Mode(time),.groups = 'drop')
# A tibble: 2 × 2
  Week       Mode
  <chr>     <dbl>
1 Thursday      0
2 Wednesday     5

If we want to insert the na.rm, it should be an argument to the function and the max should also have that argument

Test1 <- function(t, rm_na) {
  s <- table(as.vector(t))
  names(s)[s %in% max(s, na.rm = rm_na)]
   }

and use the function as

Test %>%
    group_by(Week = tools::toTitleCase(Week)) %>%   
    summarize(Mode=Test1(time, TRUE),.groups = 'drop')
  •  Tags:  
  • r
  • Related