在R语言中获取特定均值与中位数的方法
Hey Jake, great job getting that large dataset merged—8700+ rows is a solid foundation for your project! Let’s walk through exactly how to pull the average performance trend for men’s team 1, event 3 over your 6-year timeframe. We’ll use two core R packages for this: dplyr for data manipulation and ggplot2 for visualization.
Step 1: Calculate Annual Averages with dplyr
First, we’ll filter your dataset to focus only on the group you care about, then compute the average performance per year. Make sure your dataset has columns like gender, team, event, year, and time (or whatever you’ve named your performance metric—adjust column names to match your data!).
# Install packages if you haven't already (run once) install.packages(c("dplyr", "ggplot2")) # Load the dplyr package library(dplyr) # Filter target group and calculate annual averages annual_trend <- your_dataset_name %>% # Keep only men's team 1, event 3 filter(gender == "Male", team == 1, event == 3) %>% # Group the data by year group_by(year) %>% # Calculate average time (add na.rm=TRUE to ignore missing values) summarize( avg_time = mean(time, na.rm = TRUE), # Optional: Add standard deviation to measure variability std_dev_time = sd(time, na.rm = TRUE) ) # View the result print(annual_trend)
Key Notes:
- Replace
your_dataset_namewith the actual name of your merged data frame. - If your performance metric is a score (higher = better) instead of time, just swap
timewith your score column name. - The
na.rm = TRUEargument is crucial to avoid errors if you have missing data points.
Step 2: Visualize the Trend with ggplot2
Once you have the annual averages, a line plot will make the trend easy to interpret. Here’s how to build it:
# Load the ggplot2 package library(ggplot2) # Create a line plot of average time over years ggplot(annual_trend, aes(x = year, y = avg_time)) + # Add a solid line for the trend geom_line(color = "navy", linewidth = 1.2) + # Add data points to highlight each year's average geom_point(color = "orange", size = 3) + # Optional: Add error bars for standard deviation geom_errorbar(aes(ymin = avg_time - std_dev_time, ymax = avg_time + std_dev_time), width = 0.2, color = "gray") + # Customize labels and title labs( title = "Annual Average Performance: Men's Team 1, Event 3", x = "Year", y = "Average Time (Seconds)" # Adjust y-axis label if using a score metric ) + # Use a clean, readable theme theme_minimal() + # Ensure x-axis shows all 6 years clearly scale_x_continuous(breaks = unique(annual_trend$year))
What This Does:
- The line and points show how the average performance changes year-over-year.
- Error bars (if included) give context about how much performance varied within each year.
- The
scale_x_continuousline ensures every year is labeled on the x-axis, even if your data has no gaps.
Quick Troubleshooting Tips
- If you get an error about missing columns, double-check that your column names match exactly (R is case-sensitive!).
- If your
yearcolumn is stored as text instead of numbers, convert it first withmutate(year = as.numeric(year))in thedplyrchain. - If some years have no data, the
summarizestep will omit them—you can addcomplete(year)from thetidyrpackage to fill in missing years withNAif needed.
内容的提问来源于stack exchange,提问作者Jake

