gpt4 book ai didi

r - 如何修复损坏的 grouped_df?

转载 作者:行者123 更新时间:2023-12-02 04:11:12 25 4
gpt4 key购买 nike

我正在使用 R 中的 amt 包教程中的 GPS 跟踪数据。(该代码也可用 here 称为 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

产生错误消息

Error: .data is a corrupt grouped_df, the "groups" attribute must be a data frame

这似乎是最近更新 dplyr 的结果这样 temp 对象就不再适用于后面的函数。

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

谢谢

最佳答案

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

关于r - 如何修复损坏的 grouped_df?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55277761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com