I have the following list structure
ts1 <- ts(rnorm(10))
l <- list()
l[["P1"]][["M1"]][["forecast"]] <- rnorm(1)
l[["P1"]][["M1"]][["accuracy"]] <- rnorm(1)
l[["P1"]][["M1"]][["forecast"]] <- rnorm(1)
l[["P1"]][["M1"]][["accuracy"]] <- rnorm(1)
l[["P2"]][["M2"]][["forecast"]] <- rnorm(1)
l[["P2"]][["M2"]][["accuracy"]] <- rnorm(1)
l[["P2"]][["M2"]][["forecast"]] <- rnorm(1)
l[["P2"]][["M2"]][["accuracy"]] <- rnorm(1)
I wish to convert it to a data frame with the following columns: Note that I only want one of the elements of the final list
df <- data.frame(Product=c("p1","p1","p2","p2"),model=c("M1","M2","M1","M2"),accuracy=c(l$P1$M1$accuracy,l$P1$M2$accuracy,l$P2$M1$accuracy,l$P2$M2$accuracy))
I tried something like this:
df <- lapply(l, function(x) do.call(rbind,x))
df <- do.call(rbind,Map(cbind,product=names(df),df))
as.data.frame(df)
But I don´t manage to get the names properly