我正在尝试计算针对不同列组合的连续观察之间的时间。我已附上我的数据here的样本。
我的数据子集如下:
head(d1) #visualize the first few lines of the data
date time year km sps pp datetime prev timedif seque
<fct> <fct> <int> <dbl> <fct> <dbl> <chr> <dbl> <dbl> <chr>
2012/06/09 2:22 2012 110 MICRO 0 2012-06-09 02:22 0 260. 00
2012/06/19 2:19 2012 80 MICRO 0 2012-06-19 02:19 1 4144 01
2012/06/19 22:15 2012 110 MICRO 0 2012-06-19 22:15 0 100. 00
2012/06/21 23:23 2012 80 MUXX 1 2012-06-21 23:23 0 33855 10
2012/06/24 2:39 2012 110 MICRO 0 2012-06-24 02:39 0 120. 00
2012/06/29 2:14 2012 110 MICRO 0 2012-06-29 02:14 0 43.7 00
位置:
pp
:哪些物种(sps
)是捕食者(编码为1),哪些是猎物(编码为0)prev
:当前观察之后的下一个pp
timedif
:当前观测值与下一个观测值之间的时间差(以秒为单位)seque
:这是顺序顺序:第一个数字是当前的pp
,第二个数字是下一个pp
要生成datetime
列,我这样做:
d1$datetime=strftime(paste(d1$date,d1$time),'%Y-%m-%d %H:%M',usetz=FALSE) #converting the date/time into a new format
要创建其他列,我使用了以下代码:
d1 = d1 %>%
ungroup() %>%
group_by(km, year) %>% #group by km and year because I don't want time differences calculated between different years or km (i.e., locations)
arrange(datetime)%>%
mutate(next = dplyr::lead(pp)) %>%
mutate(timedif = lead(as.POSIXct(datetime))-as.numeric(as.POSIXct(datetime)))
d1 = d1[2:nrow(d1),] %>% mutate(seque = as.factor(paste0(pp,prev)))
然后我可以提取序列之间的平均时间(几何平均值):
library(psych)
geo_avg = d1 %>% group_by(seque) %>% summarise(geometric.mean(timedif))
geo_avg
# A tibble: 6 x 2
seque `geometric.mean(timedif)`
<chr> <dbl>
1 00 58830. #prey followed by a prey
2 01 147062. #prey followed by a predator
3 0NA NA #prey followed by nothing (end of time series)
4 10 178361. #predator followed by prey
5 11 1820. #predator followed by predator
6 1NA NA #predator followed by nothing (end of time series)
我有一个问题,可以分为三个部分
如何计算之间的时间差:
sps
的个人(例如,一个MICRO
之后跟着下一个MICRO
所花费的时间01
= 0)或捕食者(10
= 1)的相反类别捕食者(pp
或pp
)序列的sps
(例如,食肉动物MICRO
被另一只食肉动物追随需要多长时间(pp
= 1)。00
= 0)或捕食者(11
= 1)的相同分类(pp
或pp
)序列的sps
被其他猎物(MICRO
= 0),pp
和其他猎物跟随多长时间。我希望能够按照以下方式做一些事情:
MICRO
以防万一,这是sps pp same_sps same_class opposite_class
MICRO 0 10 days 5 days 2 days
MUXX 1 15 days 20 days 12 days
etc
的输出:
dput(d1[1:10,])
答案 0 :(得分:0)
我相信您可以通过为下一个sps添加一列并将其和当前sps包括在您的group_by
d1 %>%
mutate(next_sps = lead(sps)) %>%
group_by(sps, next_sps, seque) %>%
summarise(AvgTime = mean(timedif))
# A tibble: 5 x 4
# Groups: sps, next_sps [?]
sps next_sps seque AvgTime
<fct> <fct> <chr> <dbl>
1 MICRO MICRO 00 1.19e+ 2
2 MICRO MICRO 01 4.14e+ 3
3 MICRO MUXX 00 1.00e+ 2
4 MICRO NA 00 1.01e-317
5 MUXX MICRO 10 4.32e+ 1