[R] Mean and Timeseries modelling
Dennis Murphy
djmuser at gmail.com
Fri Jul 22 18:57:54 CEST 2011
Hi:
On Fri, Jul 22, 2011 at 2:28 AM, Marko <markho1984 at googlemail.com> wrote:
> Hello,
> i have following problem and I hope you can help me a little bit
>
>
> My dataframe looks like:
>
> df
> a m d typ value
> 1950 1 1 5 -4.1
> 1950 1 2 9 2.7
> 1950 1 3 3 -1.3
> 1950 1 4 5 -1.9
> 1950 1 5 2 0.2
> 1950 1 6 8 0.5
> 1951 1 1 4 1.3
> ....
>
> It consists by daily observations from 1950- 2009.
>
> Now, I get with....
>
> for (i in df$V5)
> neu <- tapply(df[,5],list(df$V4,df$V1),mean)
>
> the yearly means of the types (1-18) for every year.
Other ways to do this, which output data frames rather than matrices,
would include
# the formula interface below works with R-2.11.0 +
aggregate(value ~ a + typ, data = df, FUN = mean)
library(plyr)
ddply(df, .(a, typ), summarise, m = mean(value))
Both would give you the 'long form' of the data. One could use the
cast() function in the reshape[2] package or the reshape() function
from the base package to convert it to 'wide' form.
>
> The new df looks like:
>
> new
> typ 1950 1951 1952 1953 1954 1955
> 1956 ... 2009
> 1 0.40588235 -0.1714286 -1.8111111 5.4000000 -0.9555556 2.65833333
> 2 -3.17777778 1.4130435 -0.9166667 -4.9000000 0.2900000 3.54285714
> 3 0.08888889 -2.0000000 -2.9666667 2.2000000 -1.8600000 -0.50000000
> ...
> 18
>
>
> Now, i would like to generate a timeseries with the means, according to the
> different types.
> For example: For all days in the year 1950 with the typ 1, I would like to
> write the mean value for Typ 1 in year 1950. In year 1951 I would like to
> write the mean value for typ 1 in 1951 etc. (for all 18 types)
>
> The output should look like as following:
>
> erg
> a m d typ value mean_typ_year
> 1950 1 1 1 -4.1 0,4
> 1950 1 2 2 2.7 Mean (Typ2 1950)
> 1950 1 3 1 -1.3 0,4
> 1950 1 4 5 -1.9 Mean (Typ5 1950)
> 1950 1 5 2 0.2 ...
> 1950 1 6 8 0.5 ...
> 1951 1 1 1 1.3 -0,17
> 1951 1 2 2 2.1 Mean (Typ2 1951)
> ....
>
> I hope you can help me by solving this problem
It sounds like you want something like ave(). One approach (untested)
might be as follows:
ddply(df, .(a, typ), transform, mean_typ_year = mean(value))
You may want to re-sort the data afterward because ddply() will sort
by a x typ combinations rather than a x m x d. You could also use the
transform() function, perhaps something like
transform(df, mean_typ_year = ave(value, list(a, typ), FUN = mean))
##------
Here's a toy example to illustrate:
df <- data.frame(a = factor(rep(LETTERS[1:3], each = 6)),
b = factor(rep(letters[1:3], each = 2)),
d = rep(1:6, 3),
val = rnorm(18, m = 40))
library('plyr')
ddply(df, .(a, b), transform, m = mean(val))
transform(df, m = ave(val, list(a, b), FUN = mean))
HTH,
Dennis
>
> Best regards,
> Marko
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Mean-and-Timeseries-modelling-tp3686326p3686326.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list