跨数据帧的所有行的平均列值 [英] average column values across all rows of a data frame

查看:91
本文介绍了跨数据帧的所有行的平均列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从这样的文件中读取的数据框:

I've got a data frame that I read from a file like this:

name, points, wins, losses, margin
joe, 1, 1, 0, 1
bill, 2, 3, 0, 4
joe, 5, 2, 5, -2
cindy, 10, 2, 3, -2.5

等。

我想在这个数据的所有行中平均列数值,在R中是否有一个简单的方法?

I want to average out the column values across all rows of this data, is there an easy way to do this in R?

例如,我想得到所有Joe的平均列值出现如下:

For example, I want to get the average column values for all "Joe's", coming out with something like

joe, 3, 1.5, 2.5, -.5


推荐答案

加载数据后:

df <- structure(list(name = structure(c(3L, 1L, 3L, 2L), .Label = c("bill", "cindy", "joe"), class = "factor"), points = c(1L, 2L, 5L, 10L), wins = c(1L, 3L, 2L, 2L), losses = c(0L, 0L, 5L, 3L), margin = c(1, 4, -2, -2.5)), .Names = c("name", "points", "wins", "losses", "margin"), class = "data.frame", row.names = c(NA, -4L))

只需使用聚合 function: / p>

Just use the aggregate function:

> aggregate(. ~ name, data = df, mean)
   name points wins losses margin
1  bill      2  3.0    0.0    4.0
2 cindy     10  2.0    3.0   -2.5
3   joe      3  1.5    2.5   -0.5

这篇关于跨数据帧的所有行的平均列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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