title: How to extract time attributes for a univariate time series?
body: <p>How can I convert a simple time series object to be of dimensions 2 by adding time attributes so that colnames can be used?</p>
  
  <h1>Edit :</h1>
  
  <p>In this <a href="https://stackoverflow.com/questions/13278972/how-to-name-columns-in-time-series-objects">question</a>, the way to refer to time series was to use colnames and suggested method was to convert it to zoo objects. In case of single time series, if I do names(tseries), it gives NULL. So to assign the names, tried using colnames which can only be used in objects of dimension 2 and constructed by adding index of dates. Here I am trying to use the time attributes already there in the list object so that I dont have to convert back and forth from zoo objects. </p>
  
  <p>If there is an alternative I will be very happy to know that. The purpose is to be able to use the name of the time series in title of plots etc. </p>
  
  <h1>Edit 2:</h1>
  
  <p>To rephrase the question, how to give name to a time series ? Using apply strips the time series attributes as asked in many questions here. Then to get back the time attributes and the name, and in a format which allows using colnames, what are the methods ? I started with series and a column of dates which allowed naming the series but in a vector format returned from the lapply(tslist, function), I need to reallocate the names. If there is a simpler way to do this, please do let me know as that is the main problem. </p>
  
  <p>A somewhat related question is <a href="https://stackoverflow.com/questions/20878615/format-a-zoo-object-with-dimnames-list-of-2">link</a> where answer by @Henrik suggests adding a dummy column to avoid coercion of zoo object to a vector to keep the it of dimension 2. </p>
  
  <p>In a specific situation where we have a list object with required time info, I followed this method. </p>
  
  <p>When using HP filter to find the cycle, I do the following </p>
  
  <pre><code>x &lt;-ts(rnorm(100), start = c(1990, 1), frequency = 12)
  
  library(mFilter)
  hp &lt;- hpfilter(x, 1600)
  c1 &lt;- hp$cycle
  
  str(c1)
   #Time-Series [1:100] from 1990 to 1998: 1.852 -0.368 -0.942 -0.756 1.006 ...
  
  str(hp) # shows that it has the attributes of time series and tsp etc. 
  </code></pre>
  
  <p>Str of main object from which subsetting is </p>
  
  <pre><code>List of 10
   $ cycle  : Time-Series [1:100] from 1990 to 1998: 1.852 -0.368 -0.942 -0.756 1.006 ...
   $ trend  : ts [1:100, 1] -0.191 -0.193 -0.193 -0.191 -0.187 ...
    ..- attr(*, "dimnames")=List of 2
    .. ..$ : NULL
    .. ..$ : chr "Series 1"
    ..- attr(*, "tsp")= num [1:3] 1990 1998 12
   $ fmatrix: num [1:100, 1:100] 0.799 -0.178 -0.156 -0.135 -0.116 ...
   $ title  : chr "Hodrick-Prescott Filter"
   $ xname  : chr "x"
   $ call   : language hpfilter(x = x, freq = 1600)
   $ type   : chr "lambda"
   $ lambda : num 1600
   $ method : chr "hpfilter"
   $ x      : ts [1:100, 1] 1.661 -0.561 -1.135 -0.947 0.819 ...
    ..- attr(*, "dimnames")=List of 2
    .. ..$ : NULL
    .. ..$ : chr "Series 1"
    ..- attr(*, "tsp")= num [1:3] 1990 1998 12
   - attr(*, "class")= chr "mFilter"
  </code></pre>
  
  <p>c1 is an object of dimension 1 because when I try to assign it a name, </p>
  
  <pre><code>colnames(c1) &lt;- "x" 
  </code></pre>
  
  <p>gives this error </p>
  
  <pre><code>Error in `colnames&lt;-`(`*tmp*`, value = "iip") : 
    attempt to set 'colnames' on an object with less than two dimensions
  </code></pre>
  
  <p>To help use name of time series, we can either convert c1 to a zoo object by adding an index or cbind it with a column of dates. </p>
  
  <p>Since the name and time attributes information is there in the starting object from which we are subsetting, is there a way to use it to extract a component with the time attributes ? </p>
  
  <h1>Edit :</h1>
  
  <p>The extracted series must have structure like that of inputted series which is there in the list object </p>
  
  <pre><code>ts [1:100, 1] 1.661 -0.561 -1.135 -0.947 0.819 ...
    ..- attr(*, "dimnames")=List of 2
    .. ..$ : NULL
    .. ..$ : chr "Series 1"
    ..- attr(*, "tsp")= num [1:3] 1990 1998 12
  </code></pre>
  
  <p>which I think can be accessed as </p>
  
  <pre><code>attributes(c1) &lt;- attributes(hp$x)
  
  colnames(c1) &lt;- "X1"  # is now possible. 
  </code></pre>
  
  <p>How can I return hp$cycle along with its attributes ? </p>
  
  <p>How can I access chr "Series 1" when allocating colnames ?</p>
  
  <pre><code>colnames(c1) 
  
  # Series 1
  </code></pre>
  
  <h1>Edit :</h1>
  
  <pre><code>## Return hp$cycle along with the time attributes so that str(h1) is as follows
  
  h1 &lt;- hp$cycle
  str(h1)
  # this gives 
  Time-Series [1:100] from 1990 to 1998: 1.852 -0.368 -0.942 -0.756 1.006 ..
  #I want it to be of this form 
  
  ts [1:100, 1] 1.661 -0.561 -1.135 -0.947 0.819 ...
        ..- attr(*, "dimnames")=List of 2
        .. ..$ : NULL
        .. ..$ : chr "Series 1"
        ..- attr(*, "tsp")= num [1:3] 1990 1998 12
  </code></pre>
  
  <p>Thanks a lot. </p>
  
  <h1>Edit :</h1>
  
  <p>These are the alternatives I used to extract the time attributes from one series and add to another series. </p>
  
  <ol>
  <li><p>Using tsp : 
  This was giving error or dimension mismatch </p>
  
  <p>tsp(h1) &lt;- tsp(x)  </p></li>
  <li><p>Using start, end, frequency information in tsp 
  Since tsp for monthly data is in fractions, didnt know how to get the starting month info 
  i.e. converting  1970.250 to 1970, 4 say, (dont know which is the starting month)
  Then I could do something like </p>
  
  <p>h1.ts &lt;- ts(h1, start =c(tsp <a href="https://stackoverflow.com/questions/13278972/how-to-name-columns-in-time-series-objects">1</a>, n), frequency = tsp[3])</p></li>
  </ol>