R time series forecast is always the same - r

I have a lot of time series and i want a forecast for every single one for ten months. For some of them it works, for the most i always get the same forecast for every month.
The time series consists monthly data. For example:
> ts(Menge[Nummer==8 & Jahr>2014 & Index<61 ], frequency=12)
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 6 225 0 114 21 25 5 256 1 6 1 8
2 13 35 180 215 20 48 20 31 283 130 3 1
3 53 31 0 142 60 76 10 28 298 29 5 14
The output of dput is:
dput(Menge[Nummer==8 & Jahr>2014 & Index<61 ])
c(6, 225, 0, 114, 21, 25, 5, 256, 1, 6, 1, 8, 13, 35, 180, 215,
20, 48, 20, 31, 283, 130, 3, 1, 53, 31, 0, 142, 60, 76, 10, 28,
298, 29, 5, 14)
When i decompose the time series i get seasonality and a trend:
> decompose(ts(Menge[Nummer==8 & Jahr>2014 & Index<61 ], frequency=12))
$x
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 6 225 0 114 21 25 5 256 1 6 1 8
2 13 35 180 215 20 48 20 31 283 130 3 1
3 53 31 0 142 60 76 10 28 298 29 5 14
$seasonal
Jan Feb Mar Apr May Jun Jul Aug Sep Oct
1 -35.142361 -30.496528 25.065972 106.899306 -32.163194 -10.371528 -57.725694 76.336806 78.878472 4.295139
2 -35.142361 -30.496528 25.065972 106.899306 -32.163194 -10.371528 -57.725694 76.336806 78.878472 4.295139
3 -35.142361 -30.496528 25.065972 106.899306 -32.163194 -10.371528 -57.725694 76.336806 78.878472 4.295139
Nov Dec
1 -63.100694 -62.475694
2 -63.100694 -62.475694
3 -63.100694 -62.475694
$trend
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 NA NA NA NA NA NA 55.95833 48.33333 47.91667 59.62500 63.79167 64.70833
2 66.29167 57.54167 59.91667 76.83333 82.08333 81.87500 83.25000 84.75000 77.08333 66.54167 65.16667 68.00000
3 68.75000 68.20833 68.70833 65.12500 61.00000 61.62500 NA NA NA NA NA NA
$random
Jan Feb Mar Apr May Jun Jul Aug
1 NA NA NA NA NA NA 6.7673611 131.3298611
2 -18.1493056 7.9548611 95.0173611 31.2673611 -29.9201389 -23.5034722 -5.5243056 -130.0868056
3 19.3923611 -6.7118056 -93.7743056 -30.0243056 31.1631944 24.7465278 NA NA
Sep Oct Nov Dec
1 -125.7951389 -57.9201389 0.3090278 5.7673611
2 127.0381944 59.1631944 0.9340278 -4.5243056
3 NA NA NA NA
$figure
[1] -35.142361 -30.496528 25.065972 106.899306 -32.163194 -10.371528 -57.725694 76.336806 78.878472 4.295139
[11] -63.100694 -62.475694
$type
[1] "additive"
attr(,"class")
[1] "decomposed.ts"
But the forecast is always the same:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
Jan 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Feb 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Mar 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Apr 4 68.41899 -47.17701 184.015 -108.3698 245.2078
May 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Jun 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Jul 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Aug 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Sep 4 68.41899 -47.17701 184.015 -108.3698 245.2078
Oct 4 68.41899 -47.17701 184.015 -108.3698 245.2078
The data are three years (2015-2017). The forecast should be for the first ten months of 2018 (so that i can prove how good is the fitting in reality).
I did about 1000 forecasts (by changing "number" i get another time series) and very often i just got the same values, sometimes the point forecast is the same, but the Lo and Hi values change a little bit, in some cases i get different values for every month.
I observed the data in some cases but can't find a reason, why in some cases the forecasts are the same and in other cases not. Especially because i get saisonality and trends by decomposing the time series.
The whole code is:
setwd("Z:/Bestellvorschlag/Lagerdrehung") #workspace festlegen
x= read.csv("Daten Aufbereitet.csv", header=TRUE, sep=";") #read the data
attach(x)
library(forecast)
Zeilenanzahl<-length(x[,1]) #number of rows
AnzahlArtikel<-x[Zeilenanzahl,1] #number of articles
ForecastMatrix<-matrix(0,9*AnzahlArtikel,8) #i want nine forecasts for every article
#with the columns Nummer, Monat,Forecast, lower80, lower 95, upper 80, upper 95, Menge
for (i in 1:AnzahlArtikel) { #do it for all numbers; each number is another product
#extract mean(point forecast), lower und upper bounds
TS<- ts(Menge[Nummer==i & Jahr>2014 & Index<61 ], frequency=12)
mean<-unlist(forecast(TS,9)[2])
upper<-unlist(forecast(TS,9)[5])
lower<-unlist(forecast(TS,9)[6])
#write the data in a matrix
for (j in 1:9) {
ForecastMatrix[9*(i-1)+j,1]<-i
ForecastMatrix[9*(i-1)+j,2]<-j
ForecastMatrix[9*(i-1)+j,3]<-mean[j]
ForecastMatrix[9*(i-1)+j,4]<-lower[j]
ForecastMatrix[9*(i-1)+j,5]<-lower[9+j]
ForecastMatrix[9*(i-1)+j,6]<-upper[j]
ForecastMatrix[9*(i-1)+j,7]<-upper[9+j]
ForecastMatrix[9*(i-1)+j,8]<-Menge[Nummer==i & Jahr==2018 & Monat==j] #the real value
}
}
#write the data in a .csv
write.table(ForecastMatrix, file = "Forecastmatrix.csv", sep= ";")

you are getting same forecast value because your data is white noise (no trend, season and cycle), if the model find such data it will simply take avg and print same result for all forecast, therefore you are getting same forecast.
By the way i think you didn't create model ex. arima.
You should first build model then forecast.

Related

Model failed to converge (lme4)

I would like to achieve the following task. Using a linear mixed model, I would like to check whether "Month" (see dat table) has a significant effect on the "Response" variable. As for some of the tanks, data comes from different months, I included it as a random factor in my model. Please note, that sampling the same tank in different months does not change the "Response" variable. For some tank-month combinations there are multiple records, as we are included the compartment of the tank that was sampled (e.g. NW =north west).
Here the data:
print(dat)
Tank Month ID Response
1 AEW1 Jul AEW01SOBFJul2008 1.80522937
2 AEW10 Jul AEW10NWBFJul2008 2.13374401
3 AEW10 Jul AEW10NWBFJul2008 2.13374401
4 AEW11 Jun AEW11SWBFJun2008 1.65010205
5 AEW14 Jun AEW14SWBFJun2008 1.75459326
6 AEW15 Jun AEW15SOBFJun2008 2.82200903
7 AEW15 Jun AEW15SOBFJun2008 2.82200903
8 AEW18 Jul AEW18SOBFJul2008 0.39349330
9 AEW19 Jul AEW19NWBFJul2008 0.65886661
10 AEW20 Jul AEW20NWBFJul2008 1.07838018
11 AEW24 Jun AEW24NOBFJun2008 2.56677635
12 AEW27 Jul AEW27SWBFJul2008 2.64019328
13 AEW27 Jul AEW27SWBFJul2008 2.64019328
14 AEW29 Jul AEW29SOBFJul2008 2.06251217
15 AEW30 Jul AEW30NWBFJul2008 1.17010646
16 AEW31 Jun AEW31SWBFJun2008 2.25518873
17 AEW32 Jun AEW32SOBFJun2008 2.38707614
18 AEW33 Jun AEW33SOBFJun2008 2.30498448
19 AEW33 Jun AEW33SOBFJun2008 2.30498448
20 AEW36 Jul AEW36NOBFJul2008 1.92368247
21 AEW37 Jun AEW37NOBFJun2008 0.99387013
22 AEW39 Jul AEW39NOBFJul2008 1.24163732
23 AEW4 Jul AEW04SWBFJul2008 1.56327732
24 AEW42 Jun AEW42SWBFJun2008 1.26012579
25 AEW44 Jun AEW44SWBFJun2008 0.75985267
26 AEW48 Aug AEW48SOBFAug2008 1.57920494
27 AEW50 Jul AEW50NOBFJul2008 0.90052629
28 AEW8 Jul AEW08NOBFJul2008 0.00000000
29 AEW8 Jul AEW08NOBFJul2008 0.00000000
30 AEW9 Jul AEW09NOBFJul2008 0.48529647
31 HEW10 Jun HEW10SWBFJun2008 0.06412823
32 HEW10 Aug HEW10SOBFAug2008 0.06412823
33 HEW12 Jul HEW12NOBFJul2008 0.00000000
34 HEW13 Aug HEW13NWBFAug2008 2.24515850
35 HEW13 Jul HEW13SOBFJul2008 2.24515850
36 HEW13 Jul HEW13NOBFJul2008 2.24515850
37 HEW13 Jun HEW13SOBFJun2008 2.24515850
38 HEW13 Jun HEW13NWBFJun2008 2.24515850
39 HEW14 Jul HEW14SOBFJul2008 1.64783184
40 HEW18 Jun HEW18NWBFJun2008 1.32435721
41 HEW18 Jun HEW18NWBFJun2008 1.32435721
42 HEW19 Jul HEW19SWBFJul2008 1.01761003
43 HEW19 Jul HEW19SWBFJul2008 1.01761003
44 HEW22 Aug HEW22SWBFAug2008 0.63861037
45 HEW23 Jun HEW23SWBFJun2008 1.38472769
46 HEW23 Jun HEW23NWBFJun2008 1.38472769
47 HEW28 Jun HEW28NOBFJun2008 1.44377199
48 HEW3 Jun HEW03SWBFJun2008 2.19793633
49 HEW3 Jul HEW03SWBFJul2008 2.19793633
50 HEW30 Aug HEW30NWBFAug2008 0.76260579
51 HEW31 Jul HEW31SWBFJul2008 1.07879539
52 HEW35 Jun HEW35NWBFJun2008 0.86098152
53 HEW35 Jun HEW35NWBFJun2008 0.86098152
54 HEW36 Aug HEW36SOBFAug2008 0.36533352
55 HEW39 Jun HEW39SOBFJun2008 0.09283168
56 HEW4 Jun HEW04SWBFJun2008 1.89046783
57 HEW41 Aug HEW41NWBFAug2008 0.31996275
58 HEW41 Aug HEW41NWBFAug2008 0.31996275
59 HEW41 Jul HEW41NWBFJul2008 0.31996275
60 HEW41 Jul HEW41NWBFJul2008 0.31996275
61 HEW42 Jul HEW42NWBFJul2008 0.53998250
62 HEW43 Jun HEW43SWBFJun2008 1.85594061
63 HEW43 Jun HEW43SWBFJun2008 1.85594061
64 HEW44 Jun HEW44SOBFJun2008 1.79972095
65 HEW44 Jun HEW44SOBFJun2008 1.79972095
66 HEW49 Jun HEW49SWBFJun2008 1.25229249
67 HEW5 Aug HEW05SWBFAug2008 0.95559764
68 HEW50 Jun HEW50NWBFJun2008 0.42309531
69 HEW50 Jun HEW50NWBFJun2008 0.42309531
70 HEW7 Jul HEW07NWBFJul2008 0.69484213
71 HEW7 Jun HEW07NWBFJun2008 0.69484213
72 HEW8 Jul HEW08SWBFJul2008 1.15617440
73 SEW1 Aug SEW01NWBFAug2008 1.90030109
74 SEW1 Sep SEW01SWBFSep2008 1.90030109
75 SEW11 Aug SEW11NWBFAug2008 2.11940912
76 SEW12 Aug SEW12SOBFAug2008 2.29658624
77 SEW12 Jul SEW12SOBFJul2008 2.29658624
78 SEW17 Aug SEW17NOBFAug2008 1.49277937
79 SEW17 Jul SEW17NOBFJul2008 1.49277937
80 SEW17 Sep SEW17NOBFSep2008 1.49277937
81 SEW17 Aug SEW17SOBFAug2008 1.49277937
82 SEW18 Aug SEW18SOBFAug2008 1.70247509
83 SEW19 Aug SEW19SOBFAug2008 2.11617036
84 SEW20 Jul SEW20SWBFJul2008 1.87718089
85 SEW20 Jul SEW20SOBFJul2008 1.87718089
86 SEW22 Aug SEW22NOBFAug2008 0.77473833
87 SEW23 Aug SEW23NWBFAug2008 0.96183454
88 SEW23 Aug SEW23NOBFAug2008 0.96183454
89 SEW24 Jul SEW24SWBFJul2008 0.64090368
90 SEW24 Jul SEW24NWBFJul2008 0.64090368
91 SEW29 Jul SEW29SOBFJul2008 1.54699664
92 SEW29 Aug SEW29SWBFAug2008 1.54699664
93 SEW29 Aug SEW29SOBFAug2008 1.54699664
94 SEW34 Aug SEW34NWBFAug2008 1.79425003
95 SEW36 Jul SEW36SOBFJul2008 1.20337761
96 SEW4 Aug SEW04SWBFAug2008 1.59611963
97 SEW40 Sep SEW40SOBFSep2008 1.36486039
98 SEW40 Aug SEW40SWBFAug2008 1.36486039
99 SEW43 Sep SEW43SOBFSep2008 1.03169382
100 SEW44 Aug SEW44SWBFAug2008 0.79705660
101 SEW45 Jul SEW45NWBFJul2008 0.34130398
102 SEW46 Aug SEW46SOBFAug2008 0.20690386
103 SEW47 Aug SEW47SWBFAug2008 0.01564703
104 SEW47 Sep SEW47SWBFSep2008 0.01564703
105 SEW48 Aug SEW48SWBFAug2008 0.46745254
106 SEW5 Aug SEW05SWBFAug2008 0.68900435
107 SEW50 Aug SEW50NWBFAug2008 1.10731406
108 SEW7 Aug SEW07SWBFAug2008 0.08552432
109 SEW8 Jul SEW08NWBFJul2008 0.18731374
The model I generated so far is: Mod1 <- lmer(Response ~ Month + (1|Tank), data=dat)
Again, I included "Tank" because we sampled some tanks in several months but that does not change the response variable. Consequently, the response variable is fixed for each tank. Nevertheless, multiple data points originate from the same tank and I tried to account for that by including it as a random factor.
Fitting Mod1 results in the following message:
Warning messages:
1: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
Model failed to converge with max|grad| = 0.306567 (tol = 0.002, component 1)
2: In checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
Model is nearly unidentifiable: very large eigenvalue
- Rescale variables?
The question is now, whether the model is overly complex and whether I can drop "Tank" as a random factor, as measuring tanks repeatedly did not have an effect on the response variable.
Thus, the question is, would a simple linear model Mod1 <- lm(Response ~ Month, data =dat) be valid? And if not, how can I solve the 2 convergence issues.
Any help is very much appreciated! :)

Converting Month character to date for time series without "0" before Month

How do I convert this data set into a time series format in R? Lets call the data set Bob. This is what it looks like
1/2013 25
2/2013 865
3/2013 26
4/2013 33
5/2013 74
6/2013 24
Are you looking for something like this....?
> dat <- read.table(text = "1/2013 25
2/2013 865
3/2013 26
4/2013 33
5/2013 74
6/2013 24
", header=FALSE) # your data
> ts(dat$V2, start=c(2013, 1), frequency = 12) # time series object
Jan Feb Mar Apr May Jun
2013 25 865 26 33 74 24
Assuming that your starting point is the data frame DF defined reproducibly in the Note at the end this converts it to a zoo series z as well as a ts series tt.
library(zoo)
z <- read.zoo(DF, FUN = as.yearmon, format = "%m/%Y")
tt <- as.ts(z)
z
## Jan 2013 Feb 2013 Mar 2013 Apr 2013 May 2013 Jun 2013
## 25 865 26 33 74 24
tt
## Jan Feb Mar Apr May Jun
## 2013 25 865 26 33 74 24
Note
Lines <- "1/2013 25
2/2013 865
3/2013 26
4/2013 33
5/2013 74
6/2013 24"
DF <- read.table(text = Lines)

Convert character to Date (Thu Jun 14 *** 2018-05-14) in r [duplicate]

This question already has an answer here:
R convert string date (e.g. "October 1, 2014") to Date format
(1 answer)
Closed 4 years ago.
I have a dataframe which is about World Cup matches that include date,location,match_name etc.
In this dataframe I want to convert date column as date in format "2018-05-06"
Here is my file;
date match_name price
1 Thu Jun 14 Russia v Saudi Arabia €453.92
2 Fri Jun 15 Egypt v Uruguay €90.00
3 Tue Jun 19 Russia v Egypt €297.45
4 Wed Jun 20 Uruguay v Saudi Arabia €95.00
and here is my expectation;
date match_name price
1 2018-05-14 Russia v Saudi Arabia €453.92
2 2018-05-15 Egypt v Uruguay €90.00
3 2018-05-19 Russia v Egypt €297.45
4 2018-05-20 Uruguay v Saudi Arabia €95.00
This sure is not the easiest way to do it, But I just wanted you to have a quick answer.
library(stringr)
library(dplyr)
Data=data.frame(date=c("Thu Jun 14","Fri Jun 15","Tue Jun 19","Wed Jun 20"),match_name=c("a","b","c","d"),price=c(1,2,3,4))
Data$date=as.character(Data$date)
regexp <- "[[:digit:]]+"
Data=mutate(Data,datenum=str_extract(Data$date, regexp))
Data=mutate(Data,monthnum=str_extract(Data$date, regexp))
Data=mutate(Data,monthname=str_extract(Data$date,"Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"))
Data=mutate(Data,monthnum=if(Data$monthname=="Jan")
"01"
else if(Data$monthname=="Feb")
"02"
else if(Data$monthname=="Mar")
"03"
else if(Data$monthname=="Apr")
"04"
else if(Data$monthname=="May")
"05"
else if(Data$monthname=="Jun")
"06"
else if(Data$monthname=="Jul")
"07"
else if(Data$monthname=="Aug")
"08"
else if(Data$monthname=="Sep")
"09"
else if(Data$monthname=="Oct")
"10"
else if(Data$monthname=="Nov")
"11"
else if(Data$monthname=="Dec")
"12"
)
mutate(Data,Final_Date=paste0("2018-",monthnum,"-",datenum))
Resulting in
date match_name price datenum monthnum monthname Final_Date
1 Thu Jun 14 a 1 14 06 Jun 2018-06-14
2 Fri Jun 15 b 2 15 06 Jun 2018-06-15
3 Tue Jun 19 c 3 19 06 Jun 2018-06-19
4 Wed Jun 20 d 4 20 06 Jun 2018-06-20
OK, let's say you have this data.frame:
myDF <-as.data.frame(x=list(date=c("Thu Jun 14","Fri Jun 15","Tue Jun 19","Wed Jun 20")))
Which constructs the following data.frame:
date
1 Thu Jun 14
2 Fri Jun 15
3 Tue Jun 19
4 Wed Jun 20
Assuming that each game is in 2018:
#for handling month abbreviations in English:
Sys.setlocale("LC_TIME", "en_US.UTF-8")
myDF$date <- as.Date(paste0(substr(myDF$date,5,10),", 2018"),format="%b %d, %Y")
The resulting myDF:
date
1 2018-06-14
2 2018-06-15
3 2018-06-19
4 2018-06-20
You can change 2018 to any year you like where necessary.
To convert a variable "date" to the format '2018-05-14', you need to perform the following function:
conv_date <- function(var, year){
var <- as.Date(paste0(var, " ", year), '%a %b %d %Y')
return(var)
}
where:
var - variable in your data table (i.e 'date')
year - the year you need
Example:
yours_df$date <- conv_date(yours_df$date, 2018)

Date formatting MMM-YYYY

I have a dataset with dates in following format:
Initial:
Jan-2015 Apr-2013 Jun-2014 Jan-2015 Jan-2016 Jan-2015 Jan-2016 Jan-2015 Apr-2012 Nov-2012 Jun-2013 Sep-2013
Final:
Feb-2014 Jan-2013 Sep-2014 Apr-2013 Sep-2014 Mar-2013 Aug-2012 Apr-2012 Oct-2012 Oct-2013 Jun-2014 Oct-2013
I would like to perform these steps:
create dummy variables for Month and Year
Subtract these dates from another dates to find out duration (final- initials) in months
I would like to do these in R?
You could use as.yearmon from the zoo package for this.
library(zoo)
12 * (as.yearmon("Jan-2015", "%b-%Y") - as.yearmon("Feb-2014", "%b-%Y"))
# result
# [1] 11
To expand on #neilfws answer, you can use the month and year functions from the lubridate package to create your dummy variables with the month and year in your data frame.
Here is the code:
library(lubridate)
library(zoo)
df <- data.frame(Initial = c("Jan-2015", "Apr-2013", "Jun-2014", "Jan-2015", "Jan-2016", "Jan-2015",
"Jan-2016", "Jan-2015", "Apr-2012", "Nov-2012", "Jun-2013", "Sep-2013"),
Final = c("Feb-2014", "Jan-2013", "Sep-2014", "Apr-2013", "Sep-2014", "Mar-2013",
"Aug-2012", "Apr-2012", "Oct-2012", "Oct-2013", "Jun-2014", "Oct-2013"))
df$Initial <- as.character(df$Initial)
df$Final <- as.character(df$Final)
df$Initial <- as.yearmon(df$Initial, "%b-%Y")
df$Final <- as.yearmon(df$Final, "%b-%Y")
df$month_initial <- month(df$Initial)
df$year_intial <- year(df$Initial)
df$month_final <- month(df$Final)
df$year_final <- year(df$Final)
df$Difference <- 12*(df$Initial-df$Final)
And here is the final data.frame:
> head(df)
Initial Final month_initial year_intial month_final year_final Difference
1 Jan 2015 Feb 2014 1 2015 2 2014 11
2 Apr 2013 Jan 2013 4 2013 1 2013 3
3 Jun 2014 Sep 2014 6 2014 9 2014 -3
4 Jan 2015 Apr 2013 1 2015 4 2013 21
5 Jan 2016 Sep 2014 1 2016 9 2014 16
6 Jan 2015 Mar 2013 1 2015 3 2013 22
Hope this helps!

No of monthly days between two dates

diff(seq(as.Date("2016-12-21"), as.Date("2017-04-05"), by="month"))
Time differences in days
[1] 31 31 28
The above code generates no of days in the month Dec, Jan and Feb.
However, my requirement is as follows
#Results that I need
#monthly days from date 2016-12-21 to 2017-04-05
11, 31, 28, 31, 5
#i.e 11 days of Dec, 31 of Jan, 28 of Feb, 31 of Mar and 5 days of Apr.
I even tried days_in_month from lubridate but not able to achieve the result
library(lubridate)
days_in_month(c(as.Date("2016-12-21"), as.Date("2017-04-05")))
Dec Apr
31 30
Try this:
x = rle(format(seq(as.Date("2016-12-21"), as.Date("2017-04-05"), by=1), '%b'))
> setNames(x$lengths, x$values)
# Dec Jan Feb Mar Apr
# 11 31 28 31 5
Although we have seen a clever replacement of table by rle and a pure table solution, I want to add two approaches using grouping. All approaches have in common that they create a sequence of days between the two given dates and aggregate by month but in different ways.
aggregate()
This one uses base R:
# create sequence of days
days <- seq(as.Date("2016-12-21"), as.Date("2017-04-05"), by = 1)
# aggregate by month
aggregate(days, list(month = format(days, "%b")), length)
# month x
#1 Apr 5
#2 Dez 11
#3 Feb 28
#4 Jan 31
#5 Mrz 31
Unfortunately, the months are ordered alphabetically as it happened with the simple table() approach. In these situations, I do prefer the ISO8601 way of unambiguously naming the months:
aggregate(days, list(month = format(days, "%Y-%m")), length)
# month x
#1 2016-12 11
#2 2017-01 31
#3 2017-02 28
#4 2017-03 31
#5 2017-04 5
data.table
Now that I've got used to the data.table syntax, this is my preferred approach:
library(data.table)
data.table(days)[, .N, .(month = format(days, "%b"))]
# month N
#1: Dez 11
#2: Jan 31
#3: Feb 28
#4: Mrz 31
#5: Apr 5
The order of months is kept as they have appeared in the input vector.

Resources