I have a data frame that records the moon start time and moon end time for several years. I calculated the moon middle time from these two columns and used the following code to remove the date info from the moon middle time (mm), only leaving the hh:mm format.
diel$mm <- as.POSIXct((as.numeric(diel$`moonend date time`) + as.numeric(diel$`moonstart date time`)) / 2, origin = '1970-01-01')
#mm=moonlight midpoint
diel$mm <-strftime(diel$mm, tz = "UCT", format = "%H:%M")
#delete the date info on the moonlight midpoint column
Then I wanted to recode my moon middle time into numbers 1-18. My 24 hrs is separated from 9:45 am to the next day 9:45 am. and I am coding every 80 min into one time slot, for example, 9:45 to 11:05 is coded "1", 11:05 to 12:25 is coded "2",...., 8:25 to 9:45 is coded "18".
I used case_when in dplyr to recode my time variable:
diel <- diel %>% mutate(mm = case_when(
mm > 9:45 & mm < 11:05 ~ "1",
mm > 11:05 & mm < 12:25 ~ "2",
mm > 12:25 & mm < 13:45 ~ "3",
mm > 13:45 & mm < 15:05 ~ "4",
mm > 15:05 & mm < 16:25 ~ "5",
mm > 16:25 & mm < 17:45 ~ "6",
mm >17:45 & mm < 19:05 ~ "7",
mm > 19:05 & mm < 20:25 ~ "8",
mm > 20:25 & mm < 21:45 ~ "9",
mm > 21:45 & mm < 23:05 ~ "10",
mm > 23:05 & mm < 0:25 ~ "11",
mm > 0:25 & mm < 1:45 ~ "12",
mm > 1:45 & mm < 3:05 ~ "13",
mm > 3:05 & mm < 4:25 ~ "14",
mm > 4:25 & mm < 5:45 ~ "15",
mm > 5:45 & mm < 7:05 ~ "16",
mm > 7:05 & mm < 8:25 ~ "17",
mm > 8:25 & mm < 9:45 ~ "18",
is.na(mm) ~ "na"))
#I have nas in the data frame because there is one day that has no moonlight.
I am not completely understanding this case_when code: it seems " ~ "number" " is what I want to recode into, but the code before "~" is confusing me a lot. In the end, this code didn't work out (of course). As I changed the part before "~", I got different error messages.
I am not sure if my code writing is wrong, or since this moon middle time is actually containing date information (even though I removed date info) so it couldn't be recoded into 1-18. Is there any other ways I could recode my time into 1-18? Could someone help me with this? Thank you!
Your time format does not look right, eg 9:45. If you write that in the R console it produces a sequence of integers from 9 to 45. Investigate what time format is appropriate for your particular problem. Suggestion, type help("DateTimeClasses") in the R console to read the documentation around some important classes representing dates and times.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.