質問

I have a data frame like this (1704*126):

date           precip.1            precip.2       precip.3    ...   precip.125
1871-01-01     2.68                1.85           2.87              1.03
1871-02-01     1.06                1.06           2.82              3.56
1871-03-01     3.89                2.69           2.03              2.14
...
2012-12-01     0.05                3.24           1.26              1.00

1704 is the number of months between 01/1871 and 12/2012 and I have 125 experiments.

I want to have the sum of each month by year, for each experiment so a data frame like this (142*126):

date           precip.1           precip.2      precip.3 ...     precip.125
1871           586                365           456              439
1872           875                605           453              514
1873           601                753           605              613
...
2012           919                813           703              405

I try to use aggregate or summarize (from dplyr) but I have an annual mean of all experiments and not for each one of them...

Can someone help me?

役に立ちましたか?

解決

No reproducible example, so only a verbose description:

1) extract the year

2a) use ddply(data, .(year), numcolwise(mean))

or alternatively:

2b) use reshape2:::melt in combination with ddply(melteddata, .(year, variable), summarize, mean=mean(value))

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top