# Sample data on a copy/paste friendly format
sample_df <- data.frame(
stringsAsFactors = FALSE,
Year = c(1, 1, 2, 3),
Uni = c("b", "b", "b", "c")
)
# Using base R
mean(sample_df[sample_df$Uni == "b",]$Year)
#> [1] 1.333333
# Using dplyr
library(dplyr)
sample_df %>%
group_by(Uni) %>%
summarise(mean = mean(Year))
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 2 x 2
#> Uni mean
#> <chr> <dbl>
#> 1 b 1.33
#> 2 c 3