I'm surprised I couldn't find a question on this website that would answer mine.
I want to create 24 dummy variables for each hour of the day (the value is 1 if the time is that hour of the day and 0 otherwise). A (really) small part of the data looks like this:
df <- as.POSIXct(c("08-01-2018 19:46", "08-01-2018 19:50", "08-01-
2018 20:46", "09-01-2018 21:17"), format = "%d-%m-%Y %H:%M")
[1] "2018-01-08 19:46:00 CET" "2018-01-08 19:50:00 CET" "2018-01-08
20:46:00 CET" "2018-01-09 21:17:00 CET"
I want the output to be like this:
19 20 21
1: 1 0 0
2: 1 0 0
3: 0 1 0
4: 0 0 1
I have already looked at this question: Creating a dummy variable for certain hours of the day
The only problem I have with the answer for my problem is that I have to write 24 ifelse statements for each case.
I was wondering if there is a more elegant way to get this output without having to write 24 ifelse statements.
If this question is a duplicate, please let me know!
Thanks in advance,
RC