R语言以iris数据为例以species为分组计算均值
你可以使用R语言中的aggregate()函数来计算以species为分组的均值。以下是一个示例代码:
# 加载iris数据集
data(iris)
# 使用aggregate函数计算以species为分组的均值
result <- aggregate(. ~ species, data = iris, mean)
# 打印结果
print(result)
运行这段代码后,你将得到类似以下的输出结果:
species Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 5.006 3.428 1.462 0.246
2 versicolor 5.936 2.770 4.260 1.326
3 virginica 6.588 2.974 5.552 2.026
这个结果显示了每个species的Sepal.Length、Sepal.Width、Petal.Length和Petal.Width的均值
原文地址: https://www.cveoy.top/t/topic/hPr5 著作权归作者所有。请勿转载和采集!