0

I am running an arima model the library forecast, the output of this model consists in something like this:

+----------+----------------+------------+----------+-----------+----------+
|          | Point Forecast |   Lo 80    |  Hi 80   |   Lo 95   |  Hi 95   |
+----------+----------------+------------+----------+-----------+----------+
| 2016.261 |       335.0697 | 267.368566 | 402.7707 | 231.52977 | 438.6095 |
| 2016.281 |       346.7667 | 234.935713 | 458.5978 | 175.73594 | 517.7975 |
| 2016.300 |       296.3013 | 174.495528 | 418.1070 | 110.01547 | 482.5870 |
| 2016.319 |       379.0095 | 255.265230 | 502.7537 | 189.75899 | 568.2600 |
+----------+----------------+------------+----------+-----------+----------+

What I would like to achieve is to convert the decimal date (for example 2016.261), by adding two columns, one representing the year and the other one the number of week, achieveing something like this:

+----------+---------+------+----------------+------------+----------+-----------+----------+
|          |  year   | week | Point Forecast |   Lo 80    |  Hi 80   |   Lo 95   |  Hi 95   |
+----------+---------+------+----------------+------------+----------+-----------+----------+
| 2016.261 |    20.. | n1   |       335.0697 | 267.368566 | 402.7707 | 231.52977 | 438.6095 |
| 2016.281 |    20.. | n1   |       346.7667 | 234.935713 | 458.5978 | 175.73594 | 517.7975 |
| 2016.300 |    20.. | n3   |       296.3013 | 174.495528 | 418.1070 | 110.01547 | 482.5870 |
| 2016.319 |    20.. | n4   |       379.0095 | 255.265230 | 502.7537 | 189.75899 | 568.2600 |
+----------+---------+------+----------------+------------+----------+-----------+----------+
sanna
  • 1,398
  • 5
  • 16
  • 24
  • What does n1, n2, etc mean? – Sonny Mar 31 '19 at 11:51
  • The number of the week I don't know which week number of 2016 .261 is, so I used a general notation. – sanna Mar 31 '19 at 11:52
  • Why first 2 rows have same week n1? – Sonny Mar 31 '19 at 12:03
  • My mistake, sorry. – sanna Mar 31 '19 at 12:03
  • It looks like you may not have noticed that there are many better ways to provide data on a coding platform such as *Stack Overflow*, please read and learn [**How to make a great R reproducible example**](https://stackoverflow.com/a/5963610/6574038). You should also show what you have tried and where we could help you and don't expect us to supply you with ready-made code. Cheers! – jay.sf Mar 31 '19 at 12:39

1 Answers1

0

Well, with dataframe like this for example:

df1 <- data.frame(x =c(2016.01, 2016.32, 2016.261, 2016.281 , 2016.300 , 2016.319))
df1$date <- as.Date(as.character(df1$x), format="%Y.%j")
df1$year <- format(df1$date, "%Y")
df1$week <- format(df1$date, "%W")
df1

#         x       date year week
# 1 2016.010 2016-01-01 2016   00
# 2 2016.320 2016-02-01 2016   05
# 3 2016.261 2016-09-17 2016   37
# 4 2016.281 2016-10-07 2016   40
# 5 2016.300 2016-01-03 2016   00
# 6 2016.319 2016-11-14 2016   46

NB: I added first two dates just to check that the dates were correct. And istead of df1 you can use your dataframe. All information is actually from here.

Oka
  • 1,318
  • 6
  • 11