Package: dplyr


Function: summarise()


1. Calculate a mean value for item1 across all students

Review the data (d2)

# A tibble: 5 x 4
  stu_id item1 item2 item3
   <dbl> <dbl> <dbl> <dbl>
1   1234     3     2     4
2   2345     4     3     1
3   3456    NA     1     1
4   4567     4     5     1
5   5678     1     3     2

Calculate a mean for item1

  • Note: The default for base::mean() is to not calculate a sum if any NA value exists. If you want to still calculate a sum despite missing values, you can add the argument na.rm = TRUE.
d2 %>%
  summarise(item1_mean = mean(item1, na.rm = TRUE))
# A tibble: 1 x 1
  item1_mean
       <dbl>
1          3

2. Calculate a sum value for n_students across all schools

Review the data (d9)

# A tibble: 4 x 2
  sch_id n_students
   <dbl>      <dbl>
1     10        150
2     11        223
3     12        189
4     13        215

Calculate the total number of students across all schools.

d9 %>% 
  summarise(total_students = sum(n_students, na.rm = TRUE))
# A tibble: 1 x 1
  total_students
           <dbl>
1            777

Return to Calculate Sums and Means