-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateTimeSnippets.R
52 lines (46 loc) · 1.44 KB
/
dateTimeSnippets.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# add weekdays to a DT
DT$r_wday <- as.POSIXlt(DT$t_datetime)$wday # 0 = Sunday
DT$r_dow <- factor(DT$r_dow,
labels = c(
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
)
)
# add months
DT$r_month <- as.POSIXlt(DT$r_epstart)$mon # 0 = January
DT$r_month <- factor(DT$r_month,
labels = c(
"January", # 0
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" # 11 !
)
)
# add seasons
DT$season <- ifelse(DT$r_month > 1 & DT$r_month < 5, # Mar,Apr,May
"Spring", NA
)
DT$season <- ifelse(DT$r_month > 4 & DT$r_month < 8, # Jun,Jul,Aug
"Summer", DT$season
)
DT$season <- ifelse(DT$r_month > 7 & DT$r_month < 11, # Sep, Oct, Nov
"Autumn", DT$season
)
DT$season <- ifelse(DT$r_month == 11 | # Dec, Jan, Feb
DT$r_month == 0 |
DT$r_month == 1 ,
"Winter", DT$season
)