在R

时间:2017-05-03 09:31:56

标签: r loops if-statement while-loop gps

我正在尝试制作循环以计算全部速度"经度,纬度和时间戳"点。但循环不起作用。根据速度的最小值和最大值,我将过滤点并将其保存在其他矢量中。我只想在一个满足速度条件的向量中保存纬度和经度,并在其他向量中保存剩余的经度和经度。谢谢

i = 1
j = i + 1
distt <<- 0
time_TT <- 0
#Speed 
MIN <- 0.001
MAX <- 0.002
LON_S <-0
LAT_S <-0
z<-2
LON_SS <- 0
LAT_SS <- 0 



while (i < length(longitude))
{
  library(geosphere)
  dist <- distm (c(longitude [j],Latitude[j]), c(longitude [i],Latitude[i]), 
  fun = distVincentyEllipsoid)
  distt <- append(distt, dist)
  time_vector <- time[j] - time[i]
  time_TT <- append(time_TT, time_vector*60)  # converting mints into 
  seconds 
  speed <- distt/time_TT
  speed <- round(speed, 3)

  if ((speed[z] >= MIN) & (speed[z] <= MAX))
  {
    #speed is inside the range

    LON_S  <- append(LON_S, longitude [j])
    LAT_S  <- append(LAT_S, Latitude[j]) 
    z <- z + 1
  }
    #speed is outside the range

  LON_SS  <- append(LON_SS, longitude [j])
  LAT_SS  <- append(LAT_SS, Latitude[j]) 
  z <- z + 1

  i <- j
  j <- j + 1  }


  Error Message:

  Error in if ((speed[z] >= MIN) & (speed[z] <= MAX)) { : 
  missing value where TRUE/FALSE needed



 Data 

longitude <- round(c(48.7188021250007,
                 48.7188133749999,
                 48.7188291249998,
                 48.7188336250004, 
                 48.7188291250005), 8);

 Latitude <- round(c (2.39514523661229,
                 2.39512477447308, 
                 2.39472235230961,
                 2.39467460730213,
                 2.39467460730313), 8);

 time <- strptime(c('2017-04-06 09:15:00',
               '2017-04-06 09:30:00',
               '2017-04-06 09:40:00',
               '2017-04-06 09:45:00'),"%Y-%m-%d %H:%M:%S");

0 个答案:

没有答案