[R] Assign name to object for each iteration in a loop.

David Winsemius dwinsemius at comcast.net
Fri Dec 2 05:55:43 CET 2011


On Dec 1, 2011, at 1:13 PM, lglew wrote:

> Hi R-users,
>
> I'm trying to produce decompositions of a multiple time-series,  
> grouped by a
> factor (called "area"). I'm modifying the code in the STLperArea  
> function of
> package ndvits, as this function only plots produces stl plots, it  
> does not
> return the underlying data.
>
> I want to extract the trend component of each decomposition
> ("x$time.series[,trend]), assign a name based on the factor "area".
>
> My input data look like this:
> Area is a factor, with three (but could be many more) levels.
> area
> 1
> 2
> 3
>
> Ystart=2000
>
> TS is a timeseries:
>
>  X2000049   X2000065  X2000081  X2000097  X2000113
> 1     0.2080      0.2165      0.2149     0.2314      0.2028
> 2     0.1578      0.1671      0.1577     0.1593      0.1672
> 3     0.1897      0.1948      0.2290     0.2292      0.2067
>
> Here's the function:
>
> STLpA<-function(TS, area, Ystart, period=23, nSG="5,5", DSG=0)
> {
> require (RTisean)
> for(i in 1:unique(area)){
> vi.metric=TS[area==i]
> filt.vi<-sav_gol(vi.metric,n=nSG,D=DSG)
> vi.sg<-ts(filt.vi[,1], start=Ystart,frequency=period)
> stld.tmp<-stl(vi.sg, s.window="periodic", robust=TRUE,  
> na.action=na.approx)
> stld.trend<-stld.temp$time.series[,trend]

The line above will probably fail. It is more likely to succeed with:

stld.trend<-stld.temp$time.series[ ,"trend"]

Unless you have defined a variable named `trend` with a value of  
"trend".


> }
> assign(paste("stld", i , sep= "."), stld.trend)
> vi.trend<-ls(pattern= "^stld..$")
> return(vi.trend)
> }
>
> When I call this function with  
> signal=STLpA(TS,area,Ystart=2000,period=23,
> nSG= "5,5", DSG="0"))
>
> I get this error:
>
> Error in cat(list(...), file, sep, fill, labels, append) :

You might want to use traceback() at this point to see which of hte  
functions you did call is sending lists to `cat`.


>  argument 1 (type 'list') cannot be handled by 'cat'
> In addition: Warning message:
> In 1:unique(area) :
>  numerical expression has 3 elements: only the first used
>
> I'm guessing this is because I'm assigning names to each temporary  
> stl.trend
> file incorrectly.

I'm guessing it's not that since I think that step would fail if you  
got to it for the reasons described above.

> Can anyone
> improve on my rather poor efforts here?
>
> Many thanks,
>
> Louise
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Assign-name-to-object-for-each-iteration-in-a-loop-tp4129752p4129752.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list