Reputation: 143
Need some help with this code. It seems that the problem is in the TRUE condition in which I intend to indicate that it is NULL. I've already tested several alternatives but I couldn't find a solution.
library(lubridate)
library(hms)
df1 <- Sample1 %>%
mutate(Start_day = format(StartSession,format = "%d"),
End_day = format(EndSession,format ="%d"),
Dif_days = if_else (Start_day == End_day,0,1),
StartSession_Time1 = hms::as_hms(StartSession),
EndSession_Time1 = if_else(Dif_days == 0, hms::as_hms(EndSession), as_hms("23:59:59")),
StartSession_Time2 = if_else(Dif_days == 0, NULL, as_hms("00:00:00")),
EndSession_Time2 = if_else(Dif_days == 0, NULL, hms::as_hms(EndSession)))
Error message:
Error: Problem with mutate()
input StartSession_Time2
. x false
must be a logical vector, not a hms/difftime
object. ?? Input StartSession_Time2
is `if_else(Dif_days == 0, NA, as_hms("00:00:00"))
Tks in advance
Upvotes: 1
Views: 316
Reputation: 93908
NULL
is a 0-length object with class
"NULL" , so it can't be used as an output to your if_else()
call, (or base R's ifelse
for that matter).
You need to match the hms
class for both the TRUE and FALSE conditions if using if_else
, like:
data.frame(x = 1:10) %>%
mutate(out = if_else(x > 5, as_hms(NA), as_hms("00:00:00")) )
# x out
#1 1 00:00:00
#2 2 00:00:00
#3 3 00:00:00
#4 4 00:00:00
#5 5 00:00:00
#6 6 NA
#7 7 NA
#8 8 NA
#9 9 NA
#10 10 NA
Upvotes: 1