R语言将100个学生年龄分段分为5-1010-2020 统计身高随不同年龄段的变化并做折线图
假设有一个数据框叫做"student",其中包含了100个学生的年龄和身高。可以按照如下的步骤进行分段和统计:
- 创建一个新的变量"age_group",根据学生的年龄将其分为不同的年龄段:
student$age_group <- cut(student$age, breaks = c(5, 10, 20, Inf), labels = c("5-10", "10-20", ">20"))
- 使用dplyr包进行分组统计,计算每个年龄段的平均身高:
library(dplyr)
height_by_age <- student %>%
group_by(age_group) %>%
summarize(mean_height = mean(height))
- 使用ggplot2包绘制折线图:
library(ggplot2)
ggplot(height_by_age, aes(x = age_group, y = mean_height)) +
geom_line() +
labs(x = "Age Group", y = "Mean Height")
``
原文地址: https://www.cveoy.top/t/topic/gwNJ 著作权归作者所有。请勿转载和采集!