Finding the mean of a column for specific row

I don't know how to find the mean of a specific column.

The dataset is like this but definitely has more data. How can I find the average year of b in this dataset?
Year Uni
1 b
1 b
2 b
3 c

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

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.