I have a panel data set with a large number of groups. I calculated fitted values for each group and I would like to combine all fitted values into a new data set. I am looking for a possible shortcut to avoid having to do this manually.
The following data set is similar to the one I am working on (much smaller scale in terms of groups though).
set.seed(999)
dt <- data.frame("Group"=rep((LETTERS[1:10]), each=15),
"Year"=2001:2015,"value"=5+rnorm(150, 3,1))
names(dt)
head(dt)
table(dt$Year, dt$Group)
library(reshape2)
dt_tbl1 <- dcast(dt,Year~Group)
dt_tbl1
library(forecast)
tsMat <- ts(dcast(dt, Year ~ Group), start=2001, freq=1)
dt_ses <- lapply(tsMat, function(x) ses(x))
I am looking for some help to automate the following step. Add all remaining groups in the data frame.
dt_tbl2 <- data.frame("Year"=2001:2015,
data.frame(dt_ses$A$fitted),
data.frame(dt_ses$B$fitted),
data.frame(dt_ses$C$fitted))
And rename the variables in the new data set to relate to the original groups
names(dt_tbl2)[2:4] <- c("A_hat", "B_hat", "C_hat")
Once this is completed the dt_tbl2 should have the same format like the dt_tbl1.
I have tried to use sapply() and lapply() but nothing seems to be working. Thanks TCS