汇总和交叉制表数据的问题 [英] Problems with aggregating and crosstabulating data
问题描述
我的数据如下所示,大约有3300行数据:
My data looks like this, ca 3300 rows of data:
Year Location Catch
1991 0313 45100
1989 0711 323
1991 0312 1100
1991 0313 45100
1989 0711 323
1991 0312 400
1990 0313 101000
1981 0711 623
1999 0312 410
2000 0313 145100
1987 0711 323
1987 1285 770
....
年份涵盖1977-2015年,并且大约有500个不同的位置,并非每年都有数据。
Years cover the period 1977-2015, and there are approx 500 different locations, not all having data in every year.
我需要这样的输出,总结每个单元格的捕获量,并按位置(行)和年份(列)列出:
I need an output like this, summing up the catch for each cell, tabulated by location (rows) and year (columns):
Location '1977' '1978' '1979' '1980' '1981' '1982' '1983' ...
0312 456 11100 12560 320 4566 0 12010 ...
0313 121 100 4500 760 112 12050 100100 ...
0711 5500 6500 0 1205 1201 560 90500 ...
0712 325 215 600 10100 0 4500 11050 ...
1285 10005 2700 1900 101000 50 7500 6050 ...
... ... ... ... ... ... ... ...
我之前曾问过类似的问题,并且尝试过针对该问题成功提出建议的解决方案的不同版本,但没有成功。这有点不同,稍微复杂一点,但是我出了点问题。
I have asked a similar question previously, and I have tried different versions of the solution suggested successfully for that question, but without success. This is a little different and slightly more complex, and I am getting something wrong.
非常感谢您的帮助。
推荐答案
您可能可以通过 xtabs
实现所需的功能,下面将按年份计算您在答案中发布的表格的每个位置的总和。
You can probably achieve what you are looking for with xtabs
which, below, calculates the sum of each location by year for the table that you posted in your answer.
xtabs(Catch ~ Location + Year, df)
Year
Location 1981 1987 1989 1990 1991 1999 2000
312 0 0 0 0 1500 410 0
313 0 0 0 101000 90200 0 145100
711 623 323 646 0 0 0 0
1285 0 770 0 0 0 0 0
数据
df <- read.table(header=TRUE, text="Year Location Catch
1991 0313 45100
1989 0711 323
1991 0312 1100
1991 0313 45100
1989 0711 323
1991 0312 400
1990 0313 101000
1981 0711 623
1999 0312 410
2000 0313 145100
1987 0711 323
1987 1285 770 ")
这篇关于汇总和交叉制表数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!