如何获得按键分组的平均值和标准偏差? [英] How can I get the average and standard deviations grouped by key?
问题描述
我需要找到这种格式的大量数据的平均值和标准偏差.我尝试使用 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屋!