简体   繁体   English

在R包“ plyr”中使用ddply创建新列

[英]Creating a new column using ddply in the R package “plyr”

I am working on an animal tracking data set, and I need to calculate the time difference between time stamps for each GPS position for each individual. 我正在研究动物跟踪数据集,并且需要为每个人计算每个GPS位置的时间戳之间的时间差。 For simplicity, my data looks like this (lets forget about the other variables for now): 为了简单起见,我的数据看起来像这样(现在暂时忘记其他变量):

ID  Time
B1  6:57
B1  6:59
B1  7:03
B1  7:10
B2  6:34
B2  6:45
B2  6:47
B2  6:48
B3  6:23
B3  6:35
B3  6:46
B3  6:47

I tried to calculate the time difference using the following: 我尝试使用以下方法计算时差:

ddply(df, "ID",transform,timediff=diff(Time))

However I get this error message: 但是我收到此错误消息:

Error in data.frame(list(ID = c(1L, 1L, 1L, 1L), Time = 8:11): data.frame中的错误(list(ID = c(1L,1L,1L,1L),时间= 8:11):
arguments imply differing number of rows: 4, 3 参数暗示不同的行数:4,3

I assume the problem is that there is no value for the first row for each Animal. 我认为问题是每个动物的第一行都没有值。 Is there a way around this? 有没有解决的办法? Any help is much appreciated. 任何帮助深表感谢。

You could use data.table 您可以使用data.table

 library(data.table)

# create a lag variable of time by ID
setDT(data)[, timediff:=c(NA, Time[-.N]), by=ID]

dt
#>     ID Time timediff
#>  1: B1 6:57       NA
#>  2: B1 6:59        8
#>  3: B1 7:03        9
#>  4: B1 7:10       10
#>  5: B2 6:34       NA
#>  6: B2 6:45        2
#>  7: B2 6:47        4
#>  8: B2 6:48        6
#>  9: B3 6:23       NA
#>  10: B3 6:35       1
#>  11: B3 6:46       3
#>  12: B3 6:47       5

We can use ave from base R 我们可以从base R使用ave

 df1$timediff <- with(df1, ave(as.numeric(Time), ID, FUN = function(x) c(NA, diff(x))))

assuming that 'Time' is of datetime class. 假设“时间”属于日期时间类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM