3

I used the STL function in the {stats} package to seasonally detrend some time series data.

I would now like the results exported to csv, but when I try

write.csv(mySTLdata, "mySTLdata.csv")

I get the error

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ""stl"" to a data.frame

If I try to just convert the STL to a data.frame, it again tells me it cannot be coerced into one.

Any ideas on how I can get this out of the R environment for use elsewhere?

1 Answer 1

7

Going to leave this for anyone else who needs it in the future, but I figured it out.

To coerce an stl class into a data.frame, simply:

mySTLdata.DF <- as.data.frame(mySTLdata$time.series)

I was leaving off the $time.series part.

1
  • 2
    Don't you lose the dates with this? I was going to suggest library(xts); as.data.frame(as.xts(mySTLdata$time.series)). Jan 13, 2015 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.