如何修复损坏的grouped_df?

时间:2019-03-21 09:56:37

标签: r dplyr

我正在使用R中amt软件包的教程中的GPS跟踪数据。(代码也可用https://idangero.us/swiper/api/#events,名为TestVignetteMovebank2018.R)

运行代码时,我从循环中得到一个错误:

#' Load libraries
library(knitr)
library(lubridate)
library(raster)
library(move)
library(amt) 
library(ggmap)
library(tibble)
library(dplyr)

#' Create a login object for a user account at movebank.org
loginStored <- movebankLogin(username="MovebankWorkshop", password="genericuserforthisexercise")

#' Get overview information about a Movebank study. Be sure to check the citation and license terms if not using your own data.

getMovebankStudy(study="Martes pennanti LaPoint New York", login=loginStored) # see study-level info

#' Load data from a study in Movebank and create a MoveStack object. For more details and options see https://cran.r-project.org/web/packages/move/index.html.
fisher.move <- getMovebankData(study="Martes pennanti LaPoint New York", login=loginStored)
head(fisher.move)

#' Create a data frame from the MoveStack object
fisher.dat <- as(fisher.move, "data.frame")

#' ### Data cleaning
ind<-complete.cases(fisher.dat[,c("location_lat", "location_long", "timestamp")])
fisher.dat<-fisher.dat[ind==TRUE,]


#' Make timestamp a date/time variable
fisher.dat$timestamp<-as.POSIXct(fisher.dat$timestamp, format="%Y-%m-%d %H:%M:%OS", tz="UTC")

#' ## Creating a track in amt
trk <- mk_track(fisher.dat, .x=location_long, .y=location_lat, .t=timestamp, id = local_identifier, 
                crs = CRS("+init=epsg:4326"))

# Now it is easy to calculate day/night with either movement track
trk <- trk %>% time_of_day()

#' Now, we can transform back to geographic coordinates
trk <- transform_coords(trk, CRS("+init=epsg:32618"))

trk.class<-class(trk)

nesttrk<-trk%>%nest(-id)

trk<-trk %>% nest(-id) %>% 
  mutate(dir_abs = map(data, direction_abs,full_circle=TRUE, zero="N"), 
         dir_rel = map(data, direction_rel), 
         sl = map(data, step_lengths),
         nsd_=map(data, nsd))%>%unnest()

trk<-trk%>% 
  mutate(
    week=week(t_),
    month = month(t_, label=TRUE), 
    year=year(t_),
    hour = hour(t_)
  )

class(trk)<-trk.class    

#' ## SSF prep

(timestats<-trk %>% nest(-id) %>% mutate(sr = map(data, summarize_sampling_rate)) %>%
    dplyr::select(id, sr) %>% unnest)

#' Time intervals range from every 2 to 15 minutes on average, depending
#' on the individual.  Lets add on the time difference to each obs.
trk<-trk %>% group_by(id) %>% mutate(dt_ = t_ - lag(t_, default = NA))

#' THIS LOOP PRODUCES THE ERROR
ssfdat<-NULL
temptrk<-with(trk, track(x=x_, y=y_, t=t_, id=id))
uid<-unique(trk$id) # individual identifiers
luid<-length(uid) # number of unique individuals
for(i in 1:luid){
  # Subset individuals & regularize track
  temp<-temptrk%>% filter(id==uid[i]) %>% 
    track_resample(rate=minutes(round(timestats$median[i])), 
                   tolerance=minutes(max(10,round(timestats$median[i]/5))))

  # Get rid of any bursts without at least 2 points
  temp<-filter_min_n_burst(temp, 2)

  # burst steps
  stepstemp<-steps_by_burst(temp)

  # create random steps using fitted gamma and von mises distributions and append
  rnd_stps <- stepstemp %>%  random_steps(n = 15)

  # append id
  rnd_stps<-rnd_stps%>%mutate(id=uid[i])

  # append new data to data from other individuals
  ssfdat<-rbind(rnd_stps, ssfdat)
}
ssfdat<-as_tibble(ssfdat)
ssfdat

产生错误消息

  

错误:.data是损坏的grouped_df,"groups"属性必须   成为数据帧

这似乎是最近对here进行更新的结果,因此temp对象不再是可用于后续功能的对象。

您知道该问题的解决方案吗?

谢谢

1 个答案:

答案 0 :(得分:1)

此问题应该在最新版本的amt中得到解决(截至昨天,在CRAN上,我相信Windows和Mac版本可能仍在等待中。)