如何按平均和标准偏差分组? [英] How can I get the average and standard deviations grouped by key?

查看:206
本文介绍了如何按平均和标准偏差分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到这种格式的大量数据的平均和标准偏差。我尝试使用Excel,但似乎没有一个简单的方法来转置列。我在Excel中缺少什么,还是应该使用Perl?

I've need to find the average and standard deviation of a large amount of data in this format. I tried using Excel but there doesn't appear to be an easy way to transpose the columns. What am I missing in Excel or should I just use Perl?

输入文件格式为:

0 123

0 234

0 456

1 657

1 234

1 543

希望将平均值和标准偏差分组,第一列:

Want result to group the averages and standard deviations by the values in the first column:

0 AvgOfAllZeros StdDevOfALlZeros

0 AvgOfAllZeros StdDevOfALlZeros

1 AvgOfAllOnes StdDevOfAllOnes

1 AvgOfAllOnes StdDevOfAllOnes

推荐答案

这在R中很容易做到。如果你的数据在一个名为 foo 的文件中,那么这段代码将做诀窍:

This is easy to do in R. If your data is in a file called foo, then this code will do the trick:

> data <- read.table("foo")
> cbind(avg=with(data, tapply(V2, V1, mean)),
+       stddev=with(data, tapply(V2, V1, sd)))
  avg   stddev
0 271 169.5553
1 478 218.8630

这篇关于如何按平均和标准偏差分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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