Ensure date/time format is maintained

Within the 'determine which is home' for loop, 'xx' is the midpoint of two datetime objects. When the midpoint is calculated to be midnight, only the date is returned. This can be replicated with:

mydates <- as.POSIXct("2018-01-01 00:00:00", tz = "UTC")
mydates
[1] "2018-01-01 UTC"

This results in 'hourofday' being NA as an hour cannot be found. By adding the suggested format wrapper, the time is maintained and 'hourofday' can be determined. It can then successfully be applied to the embedded if-statement within the loop.

mydates <- format(as.POSIXct("2018-01-01 00:00:00", tz = "UTC"), "%Y-%m-%d %H:%M:%S")
mydates
[1] "01-01-2018 00:00:00"
pull/158/head
Hannah Roberts 2021-07-23 15:11:51 +02:00 committed by JulioV
parent 5a465873c4
commit b52059b027
1 changed files with 1 additions and 1 deletions

View File

@ -45,7 +45,7 @@ function(mobmat,obj,CENTERRAD=125,MINPAUSETIME=600,tz=""){
} }
#Determine which is home (where is the night spent) #Determine which is home (where is the night spent)
for(i in 1:length(obj$ID2)){ for(i in 1:length(obj$ID2)){
xx=as.POSIXct((mobmat[obj$ID2[i],7]+mobmat[obj$ID2[i],4])/2,tz=tz,origin="1970-01-01") xx=format(as.POSIXct((mobmat[obj$ID2[i],7]+mobmat[obj$ID2[i],4])/2,tz=tz,origin="1970-01-01"), "%Y-%m-%d %H:%M:%S")
hourofday = as.numeric(strsplit(strsplit(as.character(xx),":")[[1]][1]," ")[[1]][2]) hourofday = as.numeric(strsplit(strsplit(as.character(xx),":")[[1]][1]," ")[[1]][2])
if(hourofday>=21 || hourofday<6){ if(hourofday>=21 || hourofday<6){
for(j in 1:nrowfc){ for(j in 1:nrowfc){