2

so far the result I get is as below, which is a 'tibble' format.

However, as this is a group of time series, I wish to transform this result x into a 'tsibble' format afterwords.

Anyone know what further step I need to do?

enter image description here

I tried using

x <- function_name(n.ts = 3, freq = 12, nComp = 2, n = 120,output_format = "list")
as_tsibble(x)

And the error said enter image description here

Another trying & error enter image description here The content should display something similar to this enter image description here

  • Thank you for helping! That's the thing I worried about as well. If I use as_tsibble() directly, the error said "as_tsibble() does not know how to handle the list class yet". That's the reason I wish to transform into a "tibble" format first. –  Sep 06 '20 at 18:32
  • Based on the image showed, it seems that you have `ts` objects in each of the `list` columns. I showed a solution code below (not tested without a small reproducible example with `dput`) – akrun Sep 06 '20 at 18:34
  • In the dataset created, there was no 'year' info. So, after converting to tsibble,the index for year starts from 0001 – akrun Sep 06 '20 at 18:50

1 Answers1

0

The 'x' is a list with multiple components. We can loop over the list with map, extract the 'x' component and convert it to tsibble

library(dplyr)
library(purrr)
x1 <- map(x,  ~ as_tsibble(.x$x))
x1
#$N1
# A tsibble: 120 x 2 [1M]
#      index value
#      <mth> <dbl>
# 1 0001 Jan  186.
# 2 0001 Feb  184.
# 3 0001 Mar  189.
# 4 0001 Apr  188.
# 5 0001 May  193.
# 6 0001 Jun  189.
# 7 0001 Jul  192.
# 8 0001 Aug  194.
# 9 0001 Sep  194.
#10 0001 Oct  196.
# … with 110 more rows

#$N2
# A tsibble: 120 x 2 [1M]
#      index value
#      <mth> <dbl>
# 1 0001 Jan  18.4
# 2 0001 Feb  16.0
# 3 0001 Mar  16.9
# ...

Here, the index part is created by the column name ('Month') and as there was no information regarding the 'year', it started with '0001'

data

library(gratis)
x <- generate_ts(n.ts = 3, freq = 12, nComp = 2, n = 120)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you for helping. The new error shown in update –  Sep 06 '20 at 18:38
  • @ScarlettZhao. can you update your post with `dput` of a small example so that i can test it – akrun Sep 06 '20 at 18:38
  • Sure, this is what the function I am using. ```x <- generate_ts(n.ts = 3, freq = 12, nComp = 2, n = 120)``` –  Sep 06 '20 at 18:41
  • @ScarlettZhao from which package you got `generate_ts` – akrun Sep 06 '20 at 18:41
  • Thanks, that looks so nice. Can I ask, what is ```.x``` means there? And what is the different between use ```as_tsibble(x)``` directly and use ```as_tsibble(.x$x)```? Is ```x``` here actually means the variable ```n.ts``` defined inside the function? –  Sep 06 '20 at 18:58
  • 1
    @ScarlettZhao it is a lambda function, which would be `function(x) x` here `~` is short form for `function(.x)` – akrun Sep 06 '20 at 18:59
  • Hey, still me. A quick question, why if I did not save into ```x1```, the output cannot be knit out? How can I fix this problem? Thanks !!! –  Sep 07 '20 at 06:32
  • @ScarlettZhao Your question is not clear to me. Are you saying that if you don't create the object 'x1', it is not working? – akrun Sep 07 '20 at 20:34
  • Yes, and this situation happened just in **Vignette**, instead of a new created individual normal RMD file. So wired. I feel like maybe there is some default setting is conflicting. And I have tried ```devtools::document("C:/Users/mreal/Documents/GitHub/package_name");devtools::install("C:/Users/mreal/Documents/GitHub/package_name")``` –  Sep 09 '20 at 03:50
  • @ScarlettZhao it is not clear to me. Can you post as a new question – akrun Sep 09 '20 at 03:52
  • https://stackoverflow.com/questions/63804705/in-r-the-same-code-can-be-knit-out-in-an-individual-rmd-file-but-cannot-be-kni –  Sep 09 '20 at 04:23
  • @ScarlettZhao in the post, the error seems to be related to `list` – akrun Sep 09 '20 at 04:27
  • Thank you for help again. Very appreciate. Yep, I think you are right. I also feels like in the ```if else```, I maybe cannot just write ```if (output_formate = list)`{ output_name}``. –  Sep 09 '20 at 04:30
  • Can you also include the `else` – akrun Sep 09 '20 at 04:31
  • The full content I write is ```# New content output <- if (output_format == "list") { generated.mixture.data } else if (output_format == "tsibble") { x <- generated.mixture.data map(x, ~ as_tsibble(.x$x)) } return(output) }``` –  Sep 09 '20 at 04:34
  • @ScarlettZhao Have a doubt. Supppose you don't do anything with the generated.mixture.data. Will it work? – akrun Sep 09 '20 at 04:35
  • @ScarlettZhao Can you try `x <- map(x, ~ {.x$x <- as_tsibble(.x$x); .x})` – akrun Sep 09 '20 at 04:36