Creating a distance vector - While loop and as.POSIXlt

Hello everybodey :slight_smile:
I have a dataset with snow depth measurements modelled hourly. I would like to get a vector with the values of the distance per hour from the lowest and the highest snow depth. I have tried to write a function for this. The function is accepted by R when I use as.Date. However, I cannot display the hour information in as.Data. When using the command as.POSIXlt I get as error message:

Error in while (Date <= E) { : missing value where TRUE/FALSE needed
In addition: Warning message:
All formats failed to parse. No formats found.

I hope someone can help me here. Many greetings

my current R-code 
#x = Range of values of the modelled snow depth curves; $timestamp = Date of the modelled snow height; $value = modelled Snow height
Distancevector <- function (Beginn, End, x) {
  Date = as.POSIXlt(Beginn,format ="%Y-%m-%d %H:%M:%S")
  Distances <- 1 
  i <- 1
  E <- ymd_hms(End)+ hours(25)
  E <- as.POSIXlt(E,format ="%Y-%m-%d %H:%M:%S")
  while (Date<=E){ 
    minmax <- range(x$value[x$timestamp==Datum])
    Distance <- minmax [2] - minimax [1]
    Distances[i] <- Distance
    i <- i + 1
    Date <- ymd_hms(Date) + hours(1)
    Date <- as.POSIXlt(Date,format ="%Y-%m-%d %H:%M:%S")
  }
  return(Distances)
  print(Distances)
}
DistancesV <- Distancevector(Beginn="2020-12-20 01:00:00", End="2021-01-11 01:00:00", x=test)

Do you need Anfangsdatum in your function or Beginn ?
I also don't see what role the variable i should play.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.