Calculate elements of time series/ decompose for each variable combination for loop

I need to calculate each component of the time series for each X (50 levels) and Y (80 levels) from my dataset (df).

I wanted to go with something akin to the code below, where I tried to just get the seasonality. If I can get this it should be the same for the trend and random component of the decompose.

P <- df$X

    for(y in 1:length(P)) {
  OneP <- P[y]
  AllS <- unique(df$Y[df$X== OneP])
for(i in 1:length(AllS)) {
      OneS<- AllS[i]
df$TS[df$Y == OneS & df$X== OneP] <- ts(df$Mean[df$Y == OneS & df$X 
== OneP], start = c(1999, 1), end = c(2015, 12), frequency = 12)
df$Dec[df$Y == OneS & df$X== OneP] <- decompose(ts(df$TS[df$Y == OneS & 
df$X== OneP], frequency = 12), type = c("additive"))

df$Decomposition_seasonal[df$Y == OneS & df$X== OneP] <- df$Dec([df$Y == OneS & df$X== OneP], Dec$seasonal)


But this is not working. Error message is:

Error: attempt to apply non-function

I understand that the problem might come from my attempt to put decomposition output in a column. But how else to do it? Make a new dataset for every dev in every X * Y combination?

I know that the first lines of the code work as I used it before for something else. And I know this will run and give me TS and decomposition. It's the individual components bit that I am struggling with. Any advice is deeply appreciated.

Similar data:

X       Y    Mean   Date(mY)
Tru 	A    35.6   02.2015
Fle	    A    15     05.2010
Srl     C    67.1   05.1999
Tru 	A    13.2   08.2006
Srl     B	 89     08.2006
Tru 	B	 14.8   12.2001
Fle	    A	 21.5   11.2001
Lub     D	 34.8   03.2000

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.