假设有一个数据框叫做'student',其中包含了100个学生的年龄和身高。可以按照如下的步骤进行分段和统计:

  1. 创建一个新的变量'age_group',根据学生的年龄将其分为不同的年龄段:
student$age_group <- cut(student$age, breaks = c(5, 10, 20, Inf), labels = c('5-10', '10-20', '>20'))
  1. 使用dplyr包进行分组统计,计算每个年龄段的平均身高:
library(dplyr)
height_by_age <- student %>%
  group_by(age_group) %>%
  summarize(mean_height = mean(height))
  1. 使用ggplot2包绘制折线图:
library(ggplot2)
ggplot(height_by_age, aes(x = age_group, y = mean_height)) +
  geom_line() +
  labs(x = 'Age Group', y = 'Mean Height')
R语言:学生年龄分段统计身高变化并绘制折线图

原文地址: https://www.cveoy.top/t/topic/owhS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录