Search code examples
rmeandifftime

How do you calculate means for time differences for groups in R?


I am trying to calculate the mean time for national sailing teams in a competition.

I want to group the teams together by country to calculate the mean sailing time for USA and Japan.

Here's my code, which uses the dplyr function's group_by.

Here's the data

   test <- data.frame("RACER" = c("USA",
                                   "JAPAN",
                                   "JAPAN"),
                        "TRIAL1" = c("2021-01-01",
                                      "2021-01-05",
                                      "2021-01-10"),
                       "TRIAL2" = c("2021-02-01",
                                    "2021-02-04",
                                    "2021-02-25"),
                        stringsAsFactors = FALSE)
    
    test$TRIAL1 <- as.Date(test$TRIAL1)
    test$TRIAL2 <- as.Date(test$TRIAL2)
    test$delay <- difftime(test$TRIAL2, test$TRIAL1) 
    test$delay <- as.double(test$delay)

  

Here's my code:

t <- test %>% group_by(RACER) %>% 
  summarize(mn = mean(test$delay, na.rm=T))

I get a mean of 35.7 days for both Japan and the US, which is wrong.

Any suggestions?


Solution

  • I hope this is what you are looking for:

    test %>%
      group_by(RACER) %>%
      summarise(mn = mean(delay, na.rm = TRUE))
    
    # A tibble: 2 x 2
      RACER    mn
      <chr> <dbl>
    1 JAPAN    38
    2 USA      31