Date "class" for time series

Hello,

I am tyring to plot a graph using ggplot.

This is my data:
date quits open hires

1 2016-01-01 00:00:00 0 0 0
2 2016-02-01 00:00:00 20 -18.2 -4.00
3 2016-03-01 00:00:00 20 -10.9 -8.00
4 2016-04-01 00:00:00 20 -10.9 -12
5 2016-05-01 00:00:00 20 -9.09 -16
6 2016-06-01 00:00:00 26.7 -10.9 -16
7 2016-07-01 00:00:00 20 -7.27 -24
8 2016-08-01 00:00:00 13.3 -12.7 -12
9 2016-09-01 00:00:00 26.7 -10.9 -20
10 2016-10-01 00:00:00 20 -1.82 -8.00

... with 57 more rows

data_long
date variable value
1 2016-01-01 quits 0.000000
2 2016-02-01 quits 20.000000
3 2016-03-01 quits 20.000000
4 2016-04-01 quits 20.000000
5 2016-05-01 quits 20.000000
6 2016-06-01 quits 26.666667
7 2016-07-01 quits 20.000000
8 2016-08-01 quits 13.333333
9 2016-09-01 quits 26.666667
10 2016-10-01 quits 20.000000
11 2016-11-01 quits 13.333333
12 2016-12-01 quits 20.000000
13 2017-01-01 quits 33.333333
14 2017-02-01 quits 20.000000
15 2017-03-01 quits 26.666667
16 2017-04-01 quits 26.666667
17 2017-05-01 quits 26.666667
18 2017-06-01 quits 26.666667
19 2017-07-01 quits 20.000000
20 2017-08-01 quits 13.333333
21 2017-09-01 quits 20.000000
22 2017-10-01 quits 26.666667
23 2017-11-01 quits 20.000000
24 2017-12-01 quits 20.000000
25 2018-01-01 quits 6.666667
26 2018-02-01 quits 13.333333
27 2018-03-01 quits 26.666667
28 2018-04-01 quits 26.666667
29 2018-05-01 quits 46.666667
30 2018-06-01 quits 40.000000
31 2018-07-01 quits 33.333333
32 2018-08-01 quits 46.666667
33 2018-09-01 quits 26.666667
34 2018-10-01 quits 40.000000
35 2018-11-01 quits 40.000000
36 2018-12-01 quits 46.666667
37 2019-01-01 quits 40.000000
38 2019-02-01 quits 40.000000
39 2019-03-01 quits 46.666667
40 2019-04-01 quits 33.333333
41 2019-05-01 quits 20.000000
42 2019-06-01 quits 20.000000
43 2019-07-01 quits 40.000000
44 2019-08-01 quits 33.333333
45 2019-09-01 quits 26.666667
46 2019-10-01 quits 33.333333
47 2019-11-01 quits 26.666667
48 2019-12-01 quits 40.000000
49 2020-01-01 quits 40.000000
50 2020-02-01 quits 40.000000
51 2020-03-01 quits 33.333333
52 2020-04-01 quits 26.666667
53 2020-05-01 quits 6.666667
54 2020-06-01 quits 40.000000
55 2020-07-01 quits 53.333333
56 2020-08-01 quits 40.000000
57 2020-09-01 quits 46.666667
58 2020-10-01 quits 60.000000
59 2020-11-01 quits 33.333333
60 2020-12-01 quits 40.000000
61 2021-01-01 quits 46.666667
62 2021-02-01 quits 40.000000
63 2021-03-01 quits 53.333333
64 2021-04-01 quits 66.666667
65 2021-05-01 quits 60.000000
66 2021-06-01 quits 66.666667
67 2021-07-01 quits 73.333333
68 2016-01-01 open 0.000000
69 2016-02-01 open -18.181818
70 2016-03-01 open -10.909091
71 2016-04-01 open -10.909091
72 2016-05-01 open -9.090909
73 2016-06-01 open -10.909091
74 2016-07-01 open -7.272727
75 2016-08-01 open -12.727273
76 2016-09-01 open -10.909091
77 2016-10-01 open -1.818182
78 2016-11-01 open -1.818182
79 2016-12-01 open -1.818182
80 2017-01-01 open -3.636364
81 2017-02-01 open -1.818182
82 2017-03-01 open -9.090909
83 2017-04-01 open -9.090909
84 2017-05-01 open -10.909091
85 2017-06-01 open -3.636364
86 2017-07-01 open -9.090909
87 2017-08-01 open -5.454545
88 2017-09-01 open -9.090909
89 2017-10-01 open -9.090909
90 2017-11-01 open -10.909091
91 2017-12-01 open -7.272727
92 2018-01-01 open -9.090909
93 2018-02-01 open -5.454545
94 2018-03-01 open -3.636364
95 2018-04-01 open -1.818182
96 2018-05-01 open -3.636364
97 2018-06-01 open -1.818182
98 2018-07-01 open -3.636364
99 2018-08-01 open -3.636364
100 2018-09-01 open 3.636364
101 2018-10-01 open -1.818182
102 2018-11-01 open 0.000000
103 2018-12-01 open 3.636364
104 2019-01-01 open 3.636364
105 2019-02-01 open -5.454545
106 2019-03-01 open 5.454545
107 2019-04-01 open 5.454545
108 2019-05-01 open 3.636364
109 2019-06-01 open 0.000000
110 2019-07-01 open 0.000000
111 2019-08-01 open 1.818182
112 2019-09-01 open -7.272727
113 2019-10-01 open -1.818182
114 2019-11-01 open -1.818182
115 2019-12-01 open -7.272727
116 2020-01-01 open 0.000000
117 2020-02-01 open -5.454545
118 2020-03-01 open -12.727273
119 2020-04-01 open -20.000000
120 2020-05-01 open -18.181818
121 2020-06-01 open -12.727273
122 2020-07-01 open 0.000000
123 2020-08-01 open -5.454545
124 2020-09-01 open 0.000000
125 2020-10-01 open 10.909091
126 2020-11-01 open 5.454545
127 2020-12-01 open 1.818182
128 2021-01-01 open 5.454545
129 2021-02-01 open 27.272727
130 2021-03-01 open 10.909091
131 2021-04-01 open 18.181818
132 2021-05-01 open 23.636364
133 2021-06-01 open 27.272727
134 2021-07-01 open 49.090909
135 2016-01-01 hires 0.000000
136 2016-02-01 hires -4.000000
137 2016-03-01 hires -8.000000
138 2016-04-01 hires -12.000000
139 2016-05-01 hires -16.000000
140 2016-06-01 hires -16.000000
141 2016-07-01 hires -24.000000
142 2016-08-01 hires -12.000000
143 2016-09-01 hires -20.000000
144 2016-10-01 hires -8.000000
145 2016-11-01 hires -20.000000
146 2016-12-01 hires -16.000000
147 2017-01-01 hires 0.000000
148 2017-02-01 hires -28.000000
149 2017-03-01 hires -4.000000
150 2017-04-01 hires -16.000000
151 2017-05-01 hires -4.000000
152 2017-06-01 hires -12.000000
153 2017-07-01 hires -8.000000
154 2017-08-01 hires -16.000000
155 2017-09-01 hires -20.000000
156 2017-10-01 hires -4.000000
157 2017-11-01 hires -16.000000
158 2017-12-01 hires -16.000000
159 2018-01-01 hires -4.000000
160 2018-02-01 hires -12.000000
161 2018-03-01 hires -24.000000
162 2018-04-01 hires -20.000000
163 2018-05-01 hires -16.000000
164 2018-06-01 hires 0.000000
165 2018-07-01 hires -20.000000
166 2018-08-01 hires -16.000000
167 2018-09-01 hires -4.000000
168 2018-10-01 hires -24.000000
169 2018-11-01 hires -16.000000
170 2018-12-01 hires -12.000000
171 2019-01-01 hires -36.000000
172 2019-02-01 hires -16.000000
173 2019-03-01 hires -16.000000
174 2019-04-01 hires -8.000000
175 2019-05-01 hires -12.000000
176 2019-06-01 hires -16.000000
177 2019-07-01 hires 4.000000
178 2019-08-01 hires -4.000000
179 2019-09-01 hires -12.000000
180 2019-10-01 hires -16.000000
181 2019-11-01 hires -8.000000
182 2019-12-01 hires -16.000000
183 2020-01-01 hires -12.000000
184 2020-02-01 hires -4.000000
185 2020-03-01 hires -4.000000
186 2020-04-01 hires -24.000000
187 2020-05-01 hires -32.000000
188 2020-06-01 hires -20.000000
189 2020-07-01 hires -16.000000
190 2020-08-01 hires -8.000000
191 2020-09-01 hires 0.000000
192 2020-10-01 hires -8.000000
193 2020-11-01 hires -12.000000
194 2020-12-01 hires -20.000000
195 2021-01-01 hires -24.000000
196 2021-02-01 hires -20.000000
197 2021-03-01 hires -20.000000
198 2021-04-01 hires -16.000000
199 2021-05-01 hires -20.000000
200 2021-06-01 hires -16.000000
201 2021-07-01 hires -12.000000

I want R to recognize my "date" variable as a date variable so I can break down time periods for my graph. However, it seems like my "date" colum does not have any format, not even character one, when I run "class (date)" the output is "function". I think the format is unknown for R.

Can anyone help me to format my date variable?

Thanks,
Claudia

You're data isn't a CSV file, so how are you reading it in?

The reason class(date) returns function is because you're asking for the class of R's base date function.

I do have the file in cvs.
For this post, I copied and pasted it here.
what should I do then?

thanks

If you have the data as a CSV you can read it with read_csv() from the reader package. It will make smart choices about interpreting the data types, though you can override those with the col_types parameter if its assumptions are wrong.

For example, let's say that your data file looks like this:

"ID", "DateTime", "X", "Y", "Z"
1, "2016-01-01 00:00:00", 0, 0, 0
2, "2016-02-01 00:00:00", 20, -18.2, -4.00
3, "2016-03-01 00:00:00", 20, -10.9, -8.00

You can read that into a tibble like this:

library(readr)
df <- read_csv("datafile.csv")

Now, if you examine the class of df$DateTime you'll see that that it was properly interpreted as a date type.

Thank you so much! That did work for R to read my data.
However, when I try to change the class from "character" to "date" by using:
my_date <- as.Date (date) I get this error message:

Error in as.Date.default(date) : 
  do not know how to convert 'date' to class “Date”

Do you know how I can fix it?

Thank you so much

Are you sure your date variable is a character and not already a date? When I use read_csv() on the sample data above, the result is already a POSIXct, which is Base R's date-time class.

This is my database:

turnover <- read.csv("CSVtwo.csv", header = TRUE, sep = ",")  

When I run > sapply(turnover, class)

This is what I get:

       date       quits        open       hires 
"character"   "numeric"   "numeric"   "integer"

I dont know what I am missing,

Thank you

Note that you're using read.csv(), and not the more useful read_csv() from the readr package. read_csv() is a good deal smarter than read.csv().

I suspect that your date column may contain something that can't be parsed as a date (such as the word "Date"), and that's why it was converted to a character type instead. If that's the case, the as.Date() function will complain, too, that it can't convert the data into a date type.

I suppose date is not an object on its own (vector) but a column inside a data frame, if that is the case you have to reference it this way df_name$date, obviously you have to replace df_name by the actual name of your data frame.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.