当我知道该间隔的开始和结束记录时,如何识别属于特定时间间隔的记录? (R)

时间:2013-05-02 17:01:29

标签: r intervals

所以,这是我的问题。我有一个放射性标记蜂鸟位置的数据集,我一直在关注我作为论文的一部分。正如你可能想象的那样,它们飞得很快,所以当我失去对它们的位置的记录时,我会再次找到它们。 现在我试图识别连续跟踪鸟的区段(即“丢失”区间之间的间隔)。

    ID  Type        TimeStart   TimeEnd     Limiter Starter Ender
    1   Observed    6:45:00     6:45:00     NO      Start   End 
    2   Lost        6:45:00     5:31:00     YES     NO      NO  
    3   Observed    5:31:00     5:31:00     NO      Start   NO  
    4   Observed    9:48:00     9:48:00     NO      NO      NO  
    5   Observed    10:02:00    10:02:00    NO      NO      NO  
    6   Observed    10:18:00    10:18:00    NO      NO      NO  
    7   Observed    11:00:00    11:00:00    NO      NO      NO  
    8   Observed    13:15:00    13:15:00    NO      NO      NO  
    9   Observed    13:34:00    13:34:00    NO      NO      NO  
    10  Observed    13:43:00    13:43:00    NO      NO      NO  
    11  Observed    13:52:00    13:52:00    NO      NO      NO  
    12  Observed    14:25:00    14:25:00    NO      NO      NO  
    13  Observed    14:46:00    14:46:00    NO      NO      End 
    14  Lost        14:46:00    10:47:00    YES     NO      NO  
    15  Observed    10:47:00    10:47:00    NO      Start   NO  
    16  Observed    10:57:00    11:00:00    NO      NO      NO  
    17  Observed    11:10:00    11:10:00    NO      NO      NO  
    18  Observed    11:19:00    11:27:55    NO      NO      NO  
    19  Observed    11:28:05    11:32:00    NO      NO      NO  
    20  Observed    11:45:00    12:09:00    NO      NO      NO  
    21  Observed    11:51:00    11:51:00    NO      NO      NO  
    22  Observed    12:11:00    12:11:00    NO      NO      NO  
    23  Observed    13:15:00    13:15:00    NO      NO      End 
    24  Lost        13:15:00    7:53:00     YES     NO      NO  
    25  Observed    7:53:00     7:53:00     NO      Start   NO  
    26  Observed    8:48:00     8:48:00     NO      NO      NO  
    27  Observed    9:25:00     9:25:00     NO      NO      NO  
    28  Observed    9:26:00     9:26:00     NO      NO      NO  
    29  Observed    9:32:00     9:33:25     NO      NO      NO  
    30  Observed    9:33:35     9:33:35     NO      NO      NO  
    31  Observed    9:42:00     9:42:00     NO      NO      NO  
    32  Observed    9:44:00     9:44:00     NO      NO      NO  
    33  Observed    9:48:00     9:48:00     NO      NO      NO  
    34  Observed    9:48:30     9:48:30     NO      NO      NO  
    35  Observed    9:51:00     9:51:00     NO      NO      NO  
    36  Observed    9:54:00         9:54:00     NO      NO      NO  
    37  Observed    9:55:00         9:55:00     NO      NO      NO  
    38  Observed    9:57:00     10:01:00    NO      NO      NO  
    39  Observed    10:02:00    10:02:00    NO      NO      NO  
    40  Observed    10:04:00    10:04:00    NO      NO      NO  
    41  Observed    10:06:00    10:06:00    NO      NO      NO  
    42  Observed    10:20:00    10:33:00    NO      NO      NO  
    43  Observed    10:34:00    10:34:00    NO      NO      NO  
    44  Observed    10:39:00    10:39:00    NO      NO      End 

注意:当同一行中有“开始”和“结束”时,因为非丢失时段仅包含该记录。

我能够识别开始或结束这些“非丢失”时段的记录(在“Starter”和“Ender”列下),但现在我希望能够通过为它们提供唯一标识符来识别这些时段(期间A,B,C或1,2,3等)。 理想情况下,标识符的名称将是该期间的起点名称(即ID [Starter ==“Start”])

我正在寻找类似的东西:

    ID  Type        TimeStart   TimeEnd     Limiter Starter Ender   Period

    1   Observed    6:45:00     6:45:00     NO      Start   End     1
    2   Lost        6:45:00     5:31:00     YES     NO      NO      Lost    
    3   Observed    5:31:00     5:31:00     NO      Start   NO      3
    4   Observed    9:48:00     9:48:00     NO      NO      NO      3
    5   Observed    10:02:00    10:02:00    NO      NO      NO      3
    6   Observed    10:18:00    10:18:00    NO      NO      NO      3
    7   Observed    11:00:00    11:00:00    NO      NO      NO      3
    8   Observed    13:15:00    13:15:00    NO      NO      NO      3
    9   Observed    13:34:00    13:34:00    NO      NO      NO      3
    10  Observed    13:43:00    13:43:00    NO      NO      NO      3
    11  Observed    13:52:00    13:52:00    NO      NO      NO      3
    12  Observed    14:25:00    14:25:00    NO      NO      NO      3
    13  Observed    14:46:00    14:46:00    NO      NO      End     3
    14  Lost        14:46:00    10:47:00    YES     NO      NO      Lost    
    15  Observed    10:47:00    10:47:00    NO      Start   NO      15
    16  Observed    10:57:00    11:00:00    NO      NO      NO      15
    17  Observed    11:10:00    11:10:00    NO      NO      NO      15
    18  Observed    11:19:00    11:27:55    NO      NO      NO      15
    19  Observed    11:28:05    11:32:00    NO      NO      NO      15
    20  Observed    11:45:00    12:09:00    NO      NO      NO      15
    21  Observed    11:51:00    11:51:00    NO      NO      NO      15
    22  Observed    12:11:00    12:11:00    NO      NO      NO      15
    23  Observed    13:15:00    13:15:00    NO      NO      End     15
    24  Lost        13:15:00    7:53:00     YES     NO      NO      Lost    

这在R中难以做到吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

> d <- data.frame(Limiter = rep("NO", 44), Starter = rep("NO", 44), Ender = rep("NO", 44), stringsAsFactors = FALSE)
> d$Starter[c(1, 3, 15, 25)] <- "Start"
> d$Ender[c(1, 13, 23, 44)] <- "End"
> d$Limiter[c(2, 14, 24)] <- "Yes"
> d$Period <- ifelse(d$Limiter == "Yes", "Lost", which(d$Starter == "Start")[cumsum(d$Starter == "Start")])
> d
       Limiter Starter Ender Period
1       NO   Start   End      1
2      Yes      NO    NO   Lost
3       NO   Start    NO      3
4       NO      NO    NO      3
5       NO      NO    NO      3
6       NO      NO    NO      3
7       NO      NO    NO      3
8       NO      NO    NO      3
9       NO      NO    NO      3
10      NO      NO    NO      3
11      NO      NO    NO      3
12      NO      NO    NO      3
13      NO      NO   End      3
14     Yes      NO    NO   Lost
15      NO   Start    NO     15
16      NO      NO    NO     15
17      NO      NO    NO     15
18      NO      NO    NO     15
19      NO      NO    NO     15
20      NO      NO    NO     15
21      NO      NO    NO     15
22      NO      NO    NO     15
23      NO      NO   End     15
24     Yes      NO    NO   Lost
25      NO   Start    NO     25
26      NO      NO    NO     25
27      NO      NO    NO     25
28      NO      NO    NO     25
29      NO      NO    NO     25
30      NO      NO    NO     25
31      NO      NO    NO     25
32      NO      NO    NO     25
33      NO      NO    NO     25
34      NO      NO    NO     25
35      NO      NO    NO     25
36      NO      NO    NO     25
37      NO      NO    NO     25
38      NO      NO    NO     25
39      NO      NO    NO     25
40      NO      NO    NO     25
41      NO      NO    NO     25
42      NO      NO    NO     25
43      NO      NO    NO     25
44      NO      NO   End     25