通过在同一数据框中搜索并在 R 中应用条件语句来插入具有值的列

Inserting a column with values by searching within the same dataframe, and applying conditional statements, in R

我想创建一个 dataframe,命名为 POI_gps:

数据框内大约有 100,000 个观测值。

location_gps

样本
时间戳 id 纬度
2014-01-0606:28:01 35 36.0762 24.8747
2014-01-0606:28:01 35 36.0762 24.8746
2014-01-0606:28:03 1 36.0661 24.8826
structure(list(Timestamp = structure(c(1388960881, 1388960881, 1388960883, 1388960885, 1388960886, 1388960887), tzone = "", class = c("POSIXct", "POSIXt")), id = c(35, 35, 35, 35, 35, 35), lat = c(36.0762, 36.0762, 36.0762, 36.0762, 36.0762, 36.0762), long = c(24.8747, 24.8746, 24.8744, 24.8743, 24.8742, 24.8741)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

我的想法是在3分钟后检查同一辆车是否在同一lat/long。如果是,isStay 将为 TRUE,如果不是,isStay 将为 FALSE。

我为此写了一个函数:

searchGPS <- function(gpsTime, vehID, current.lat, current.long){
  x <- POI_gps %>%
    filter(id == vehID &
             Timestamp == (gpsTime + 60*3))
  
  ifelse(dim(x), return(FALSE),
         ifelse((x$lat[1] == current.lat & x$long[1] == current.long), return(TRUE), return(FALSE)))
  
}

我试过这样做,但没有用。我是 R 的新手。

POI_gps <- locations_gps %>%
  group_by(id) %>%
  mutate("3min" = Timestamp + 60*3) %>%
  mutate("stay" = searchGPS("3min", id, lat, long))

这是我的错误:

错误:mutate()stay 有问题。 我stay = searchGPS("3min", id, lat, long)。 x filter() 输入 ..1 的问题。 我输入..1id == vehID & Timestamp == (gpsTime + 60 * 5)。 x 二元运算符的非数字参数 I 错误发生在第 1 组:id = 1。 I 错误发生在第1组:id = 1.

如果我理解你正在尝试做的事情是正确的,你可以这样做:

locations_gps |>
  mutate(min3 = Timestamp + 180) |> 
  group_by(id) |>
  mutate(stay = if_else(min3 <= lag(min3) & lat == lag(lat) & long == lag(long), TRUE, FALSE)) |> 
  ungroup()

# A tibble: 6 x 6
  Timestamp              id   lat  long min3                stay 
  <dttm>              <dbl> <dbl> <dbl> <dttm>              <lgl>
1 2014-01-05 14:28:01    35  36.1  24.9 2014-01-05 14:31:01 NA   
2 2014-01-05 14:28:01    35  36.1  24.9 2014-01-05 14:31:01 FALSE
3 2014-01-05 14:28:03    35  36.1  24.9 2014-01-05 14:31:03 FALSE
4 2014-01-05 14:28:05    35  36.1  24.9 2014-01-05 14:31:05 FALSE
5 2014-01-05 14:28:06    35  36.1  24.9 2014-01-05 14:31:06 FALSE
6 2014-01-05 14:28:07    35  36.1  24.9 2014-01-05 14:31:07 FALSE

注意变量名不能以数字开头。