如何在一个数据中获得每个数字列的平均值? [英] How can I get each numeric column's mean in one data?

查看:50
本文介绍了如何在一个数据中获得每个数字列的平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有名为 cluster_1 的数据.它具有从第一列到第三列的名义变量.

I have data named cluster_1. It has nominal variable from first column to the third.

# select the columns based on the clustering results
cluster_1 <- mat[which(groups==1),]

m_cluster_1 <-   mean(cluster_1[c(-(1:3))])

通过最后一条语句,我可以得到所有列的平均值.但是,我想要的是将每个变量(列)的平均值附加到列的底部.

By the last statement, I can get the mean of all columns'. However, what I want is to attach the mean of each variable(column) to the bottom of the column.

我该怎么做?请告诉我.

How can I make it? Please let me know.

推荐答案

colMeans() 将为您提供数据框或矩阵中每一列的均值.并且 rbind() 可用于附加结果.

colMeans() will give you the mean of each column in a data frame or matrix. And rbind() can be used to append the result.

rbind(cluster_1[, -(1:3)], colMeans(cluster_1[, -(1:3)]))

这篇关于如何在一个数据中获得每个数字列的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆