Finding the mean of a column for specific row

A couple of examples

# 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

Created on 2020-10-13 by the reprex package (v0.3.0)

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like