Calculate Conditional Mean


I am new to R so learning this is very difficult for me. I have a data set that has a number of variables that pertain to football teams. Some of these variables are: number of wins, average attendance, % conversion on 4th down, the year. I need to derive the mean number of wins for "Ohio State" in the block 2000-2016. Any and all help is appreciated. I attached a screenshot just to give you an idea of what I am working with.

Hi @jym5556 - welcome!

Assuming that you have the data in memory in R as footballdata_econ_485, you might want to try something like

footballdata_econ_485 %>%
    filter(between(year, 2000, 2016), 
           school == "Ohio State") %>%
    summarize(mean_wins = mean(wins))

which should give you a data frame with one element -- the element that you want.

Does that work?


Thanks for the help. I ran into an issue with this. It stated error in between(year, 2000, 2016) : object 'year' not found. This doesn't make sense to me as year is the column in the screenshot. Sorry, I am brand new to this and need lots of help.

Hi, welcome!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

1 Like

Thanks for the message @jym5556 - I just want to follow up a bit on @andresrcs's post above -- without enough information, it is hard for us to identify what's going on. If I had to guess, the issue is with the way you are importing data into R.

When I have an excel file, I default to using the readxl package to read in my file nicely.

I'd be curious to see how the data looks like once you read it in, before you try any formatting steps. You can do this by str(footballdata_econ_485) or dplyr::glimpse(footballdata_econ_485). If the import was correct, you will see that you have a data frame of the same number of columns/rows as in your excel, school is of type "character" or "factor", and year and wins are of type numeric or integer.

1 Like

I actually fixed it. Thanks!

1 Like

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