'Ifelse statement returning only NA
I am running this ifelse statement in R which is only producing NAs. Can you please help me with where I have gone wrong.
I am trying to identify dawn, day, dusk, night preriods of a data set. I am using the same POSIXct format for all time/date.
Below is an example of my data (Burst$starttime)
Event Start.time
1 19/05/2020 16:01:21
2 19/05/2020 16:02:49
3 19/05/2020 19:18:19
4 21/05/2020 22:44:57
5 23/05/2020 19:43:59
6 23/05/2020 19:59:29
7 23/05/2020 20:39:05
8 23/05/2020 22:12:46
9 24/05/2020 0:56:22
10 24/05/2020 1:12:56
And the 'Sunlight' data
date DawnEnd DawnStart DuskEnd DuskStart
1 19/05/2020 19/05/2020 8:17:54 19/05/2020 6:17:54 19/05/2020 18:29:34 19/05/2020 16:29:34
2 19/05/2020 19/05/2020 8:17:54 19/05/2020 6:17:54 19/05/2020 18:29:34 19/05/2020 16:29:34
3 19/05/2020 19/05/2020 8:17:54 19/05/2020 6:17:54 19/05/2020 18:29:34 19/05/2020 16:29:34
4 21/05/2020 21/05/2020 8:19:23 21/05/2020 6:19:23 21/05/2020 18:28:21 21/05/2020 16:28:21
5 23/05/2020 23/05/2020 8:20:50 23/05/2020 6:20:50 23/05/2020 18:27:13 23/05/2020 16:27:13
6 23/05/2020 23/05/2020 8:20:50 23/05/2020 6:20:50 23/05/2020 18:27:13 23/05/2020 16:27:13
7 23/05/2020 23/05/2020 8:20:50 23/05/2020 6:20:50 23/05/2020 18:27:13 23/05/2020 16:27:13
8 23/05/2020 23/05/2020 8:20:50 23/05/2020 6:20:50 23/05/2020 18:27:13 23/05/2020 16:27:13
9 24/05/2020 24/05/2020 8:21:33 24/05/2020 6:21:33 24/05/2020 18:26:42 24/05/2020 16:26:42
10 24/05/2020 24/05/2020 8:21:33 24/05/2020 6:21:33 24/05/2020 18:26:42 24/05/2020 16:26:42
11 24/05/2020 24/05/2020 8:21:33 24/05/2020 6:21:33 24/05/2020 18:26:42 24/05/2020 16:26:42
12 24/05/2020 24/05/2020 8:21:33 24/05/2020 6:21:33 24/05/2020 18:26:42 24/05/2020 16:26:42
13 24/05/2020 24/05/2020 8:21:33 24/05/2020 6:21:33 24/05/2020 18:26:42 24/05/2020 16:26:42
Burst$Sunlight <- if_else (Burst$Start.time >Sunlight$DawnStart && Burst$Start.time <Sunlight$DawnEnd, "Dawn",
if_else (Burst$Start.time > Sunlight$Dawnend && Burst$Start.time < Sunlight$DuskStart, "Day",
if_else (Burst$Start.time >Sunlight$DuskStart &&Burst$Start.time < Sunlight$DuskEnd,"Dusk",
if_else (Burst$Start.time <Sunlight$DuskEnd && Burst$Start.time >Sunlight$DawnStart, "Night", "NA" ))))
Solution 1:[1]
If you provided a sample of your dataset or similar data we could troubleshoot. However, I would strongly suggest checking out the case_when() function for multiple conditions. See for instance here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | elidomx |
