I am working with acoustic telemetry data and I am trying to further divide my "hours" column into intervals. Ideally I would like to say timeperiod1 is "early" if it falls between 00:00:01-06:00:00 etc or something of that nature. I have already used strptime and POSIXct to format date and time and make hours into their own column I am just extremely stuck on the creation and naming of this new "time period" column so I can run an ANOVA.
Thanks!
Asked
Active
Viewed 96 times
0

Dave2e
- 22,192
- 18
- 42
- 50

Hillary Ann Dean
- 3
- 1
-
For the future it would be great if you could include an example dataset that people can use to help solve your problem. Check out this FAQ for how to do that: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – qdread Apr 30 '18 at 19:04
-
Thanks I was trying to get a few lines of my dataframe in here and it kept rejecting my post – Hillary Ann Dean Apr 30 '18 at 19:53
1 Answers
1
The lubridate
package has nice functions like hour()
, year()
, etc. that extract the appropriate component of a date object. Here is an example of how you could use that to create a new column, on a toy dataset with 24 hourly times, and use ifelse()
, which is a vectorized if
, to classify a time as early if it is before 06:00:00.
fake_times <- data.frame(time = as.POSIXct('2018-04-01 00:00:01') + (0:23) * 3600)
library(lubridate)
fake_times$hour <- hour(fake_times$time)
fake_times$timeperiod <- ifelse(fake_times$hour < 6, 'early', 'late')
The output looks like this.
time hour timeperiod
1 2018-04-01 00:00:01 0 early
2 2018-04-01 01:00:01 1 early
3 2018-04-01 02:00:01 2 early
4 2018-04-01 03:00:01 3 early
5 2018-04-01 04:00:01 4 early
6 2018-04-01 05:00:01 5 early
7 2018-04-01 06:00:01 6 late
8 2018-04-01 07:00:01 7 late
9 2018-04-01 08:00:01 8 late
10 2018-04-01 09:00:01 9 late
11 2018-04-01 10:00:01 10 late
12 2018-04-01 11:00:01 11 late
13 2018-04-01 12:00:01 12 late
14 2018-04-01 13:00:01 13 late
15 2018-04-01 14:00:01 14 late
16 2018-04-01 15:00:01 15 late
17 2018-04-01 16:00:01 16 late
18 2018-04-01 17:00:01 17 late
19 2018-04-01 18:00:01 18 late
20 2018-04-01 19:00:01 19 late
21 2018-04-01 20:00:01 20 late
22 2018-04-01 21:00:01 21 late
23 2018-04-01 22:00:01 22 late
24 2018-04-01 23:00:01 23 late

qdread
- 3,389
- 19
- 36
-
so I am not sure where my error is but it is classifying everything as "early" – Hillary Ann Dean Apr 30 '18 at 14:41
-
so ideally I'd like to categorize as such:create the division of time periods early=midnight-6am day=6:01-noon mid=13:01-18:00 night=18:01-23:59 thanks for the help im closer then i was! – Hillary Ann Dean Apr 30 '18 at 16:24
-
@HillaryAnnDean In that case, you would want to do this after creating the `hour` column: `fake_times$timeperiod <- cut(fake_times$hour, breaks = c(0, 6, 12, 18, 24), right = FALSE, labels = c('early', 'day', 'mid', 'night'))`. This makes a factor with four levels split where you specified, with the labels you specified. – qdread Apr 30 '18 at 19:02
-
you are amazing thank you! i got everything completed using cast and melt and the code you gave me so all i need to figure out is my normalcy tests, plots and ANOVA and I'm done :) Ive been fooling with that code for 4 days – Hillary Ann Dean Apr 30 '18 at 20:33
-
@HillaryAnnDean FYI if you think this answer is correct please accept it so that others can see it is a good solution. – qdread Apr 30 '18 at 21:01
-