Home > Back-end >  R - average of averages
R - average of averages

Time:07-25

I have calculated the average and sd of averages succesfully with the following code on R:

 grouped_df <- group_by(datz2, profile_name)
  
  glimpse(grouped_df)
  
  summarized_df  <- summarize(grouped_df, 
                              average = mean(average_hr_times_min, na.rm = TRUE),
                              sd = sd(average_hr_times_min, na.rm = TRUE), 
                              median = median(average_hr_times_min, na.rm = TRUE))
  
  summarized_df %>% 
    ungroup %>% 
    summarize (meanofmean=mean(average))
  
  
  summarized_df %>% 
    ungroup %>% 
    summarize (meanofmean=sd(average))

However, the code seems extensive to me.

I wonder if there is a more elegant way of doing this?

Ty

CodePudding user response:

library(tidyverse)
library(magrittr)

summarized <- mtcars %>%  
  rownames_to_column("car") %>%  
  group_by(car) %>%  
  summarise(mean = mean(cyl, na.rm = TRUE),
            median = median(cyl, na.rm = TRUE))

# A tibble: 32 × 3
   car                 mean median
   <chr>              <dbl>  <dbl>
 1 AMC Javelin            8      8
 2 Cadillac Fleetwood     8      8
 3 Camaro Z28             8      8
 4 Chrysler Imperial      8      8
 5 Datsun 710             4      4
 6 Dodge Challenger       8      8
 7 Duster 360             8      8
 8 Ferrari Dino           6      6
 9 Fiat 128               4      4
10 Fiat X1-9              4      4
# … with 22 more rows

summarized %>% 
  summarise(across(2:3, ~ mean(.x)))

# A tibble: 1 × 2
   mean median
  <dbl>  <dbl>
1  6.19   6.19

CodePudding user response:

You could simply skip the substeps and pipe the results immediately:

output <- datz2 %>%
  group_by(profile_name) %>%
  summarize(average = mean(average_hr_times_min, na.rm = TRUE),
            sd = sd(average_hr_times_min, na.rm = TRUE), 
            median = median(average_hr_times_min, na.rm = TRUE)) %>%
  summarize(meanofmean=mean(average),
            meanofsd=sd(average))
  • Related