I'm not sure this is the best wording for the question (and maybe my vocabulary is leading to a lack of applicable search results within this and other help forums), but I'm hoping the example below makes sense and a solution can be proposed.
I have an ordered time series object (in this case a zoo object) that I am applying a function to using llply. The output is a list consisting of the same number of components as there are columns in the zoo object (in my case each is a site). Each of these list components is a data.frame with the same number of columns. I like llply because it preserves the names of the original object as the names of the list. However, what I'd like to do is create a column in each data.frame with the site names. My purpose is that I'd like to use a ldply later to pull information from each data.frame for further processing and the name could come in handy calling other data in the environment.
For the life of me, I can't figure out how to get the name information into the data.frame (although I feel it should be rather straightforward). I'm not exactly sure, but I think because of the splitting behaviour of plyr there is no name during the processing (although it must be retained somewhere as it is present in the list output).
Here is some dummy code that I think encapsulates the problem and provides an idea of what I'm hoping to achieve:
library(zoo)
library(plyr)
t.zoo <- merge.zoo("A" = zoo(rnorm(5),
order.by=seq_len(5)), "B" = zoo(rnorm(5),
order.by=seq_len(5)), "C" = zoo(rnorm(5),
order.by=seq_len(5)))
t.list <- llply(t.zoo, function(i) data.frame(x=i+2, y=i-2, z=i+10)) # This works
t.list <- llply(t.zoo, function(i) data.frame(x=i+2, y=i-2, z=i+10, q=names(i))) #This fails because it doesn't find the names
names(t.zoo) # This returns the names of the zoo object
names(t.zoo)[,1] # But this returns NULL because when a single column is selected (as I think llply does) it no longer appears to have a dimnames attribute
# Ideally, my output would be:
t.list.goal <- list("A" = data.frame(x=rnorm(5), y=rnorm(5), z=rnorm(5), q=rep("A", 5)), "B" = data.frame(x=rnorm(5), y=rnorm(5), z=rnorm(5), q=rep("B", 5)), "C" = data.frame(x=rnorm(5), y=rnorm(5), z=rnorm(5)), q=rep("C", 5))
Thanks for any help.