简体   繁体   English

在 R 代码中使用 na.rm=TRUE 进行汇总时出现问题

[英]Problem using na.rm=TRUE in summarize in R code

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?你能帮我理解为什么如果我这样做: summarize(Mode=Test1(time, na.rm = TRUE),.groups = 'drop')在下面的代码中,它不起作用? I just inserted na.rm=TRUE我刚刚插入了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 

If we want to find the mode, use Mode如果我们想找到模式,使用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如果我们想插入na.rm ,它应该是函数的一个参数,并且max也应该有那个参数

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM