0
votes

I am having a problem with the following date frame in which I want to pass the "vendas" column to an object of type time series.

head(base3)
# A tibble: 6 x 4
# Groups:   trimestre, ano [6]
  trimestre   ano Código.do.Produto vendas
      <int> <dbl> <fct>              <dbl>
1         3  2009 S3                  7300
2         1  2010 S3                  7900
3         2  2010 S3                 50700
4         3  2010 S3                 20900
5         2  2011 S3                 12000
6         3  2011 S3                  2300

i'm trying this:

baseaux<-base3%>%filter(ano==min(base3$ano))
ts<-ts(base3[,4],start=c(min(base3$ano),min(baseaux$trimestre)),frequency=4)

and i'm getting this:

  Qtr1   Qtr2   Qtr3   Qtr4
2009                 7300   7900
2010  50700  20900  12000   2300
2011  15000  35000  30000      0
2012  10300  35000  50000  60000
2013  90000 111400  80000  30900
2014  46400  21700  32300   2500
2015   1800   5000 300000  38000
2016  42000  68000  27900 229500
2017  12100 243000 180300 283000
2018 137000 

the date in the time series is being created on a continuous basis of time. But, I do not have the data frame data that way. How do I fix this?

1
Hi Lucas, is the answer above give you a solution ?Rémi Coulaud

1 Answers

0
votes

The output you have is only due to the print specification for object ts. In fact, you have not to worry if you have to put your ts object into your data.frame because in reality the ts object is a vector. ts object is a matrix only if you specified it and if you want a multiple time series for example one for each year. The code above works well for me :

df <- data.frame(trimestre = c(3, 1, 2, 3),
                 ano = c(2009, 2010, 2010, 2010),
                 Código.do.Produto = "S3",
                 vendas = c(7300, 7900, 50700, 20900))

baseaux <- df %>% filter(ano==min(ano))

ts <- ts(df[,4],start=c(min(df$ano),min(baseaux$trimestre)), frequency=4)

df$ts <- ts

This code is just to show you that ts how you created it is a vector see the result of str and you see the print default option is calendar=T which led you to think that the output was a matrix but it is not if you look to ts with the othe option.

print(ts)
print(ts, calendar = F)
str(ts)

I hope it will help you.