[R] Create values based on a table of conditions

John Kane jrkrideau at inbox.com
Fri Mar 29 21:28:58 CET 2013


I must be blind but I don' see any link between the two files.  RD2 seems to give some kind of encounter data in length of time.  allocate gives the time that an animal was wearing a collar ?  

If each file belongs to a specific animal (am I reading this correctly) what does it matter what the collar was?  Or is there something in the RD2 file that gives a clue to other collars ?

I must be missing the point completely but I don't understand why you want to know who was wearing what at what time from the data you have presented.  

BTW , nice presentation of the sample data however dput (head(RD2, 200)) would have been even better than head(RD2, 200).

John Kane
Kingston ON Canada


> -----Original Message-----
> From: cat.e.cowie at gmail.com
> Sent: Fri, 29 Mar 2013 18:49:45 +0800
> To: r-help at r-project.org
> Subject: [R] Create values based on a table of conditions
> 
> Hi R help forum,
> 
> I have a simple data frame of four columns - one of numbers (really a
> categorical variable), one of dates and one
> 
> of data. I have over 500,000 data points to work with, spread over 40
> files, each named after a different animal.
> 
> These are contact data recorded by proximity loggers over two years
> between the animals of the file name and
> 
> collars being worn by other animals. The collar may have been worn by
> more than one different animal in the study period.
> 
> So for example...
> 
> filename: RD2
> 
> And the data looks like:
> 
> (I'm sorry here, dput(head(RD2, 200)) was outputting all the times,
> such that it was too large for the console, so I just used head() )
> 
>> head(RD2, 200)
>     encounter_id       date encounter_start encounter_length
> 1             15 22/06/2011        22:21:45               20
> 2             10 04/07/2011        04:18:57                1
> 3             10 04/07/2011        04:23:12                1
> 4             15 08/07/2011        02:02:30               23
> 5             15 08/07/2011        02:03:26              264
> 6             15 08/07/2011        02:09:20               35
> 7             15 08/07/2011        02:10:25              173
> 8             15 08/07/2011        02:13:40               65
> 9             15 08/07/2011        02:15:12               47
> 10            15 08/07/2011        02:17:16               65
> 11            15 08/07/2011        02:39:10               54
> 12            15 14/07/2011        02:20:27                1
> 13            15 14/07/2011        02:23:35                6
> 14            15 14/07/2011        02:25:52                1
> 15            15 14/07/2011        02:26:17               39
> 16            15 14/07/2011        02:28:36               31
> 17            60 14/07/2011        02:41:56                1
> 18            60 14/07/2011        02:42:30                1
> 19            60 14/07/2011        02:43:30               36
> 20            60 14/07/2011        02:45:38                1
> 21            60 14/07/2011        02:46:07               15
> 22            60 14/07/2011        02:46:59               11
> 23            52 14/07/2011        02:57:43               97
> 24            12 09/08/2011        22:13:42                1
> 25            41 18/08/2011        02:53:18                1
> 26            41 19/08/2011        00:57:46                1
> 27            41 19/08/2011        01:59:17                1
> 28            41 19/08/2011        02:06:16               21
> 29            41 19/08/2011        02:08:50                1
> 30            41 19/08/2011        02:09:26               29
> 31            50 19/08/2011        02:19:31                1
> 32            50 19/08/2011        02:20:20                1
> 33            50 19/08/2011        02:20:54                1
> 34            50 19/08/2011        02:21:25               25
> 35            50 19/08/2011        02:22:17               20
> 36            50 19/08/2011        02:23:00               10
> 37            50 19/08/2011        02:25:10                1
> 38            50 19/08/2011        02:25:54                1
> 39            50 19/08/2011        02:26:50               75
> 40            50 19/08/2011        02:29:53                1
> 41            50 19/08/2011        02:30:41                6
> 42            50 28/08/2011        19:32:47                1
> 43            50 28/08/2011        19:36:38               10
> 44            39 30/08/2011        22:13:14                1
> 45            52 13/09/2011        20:04:57               17
> 46            20 18/09/2011        00:32:43                8
> 47            17 02/10/2011        09:10:18               52
> 48            17 02/10/2011        09:15:49                1
> 49            17 02/10/2011        09:17:19                1
> 50            25 03/10/2011        17:16:20                6
> 51            41 05/10/2011        04:04:35                1
> 52            52 07/10/2011        22:59:57               45
> 53            62 16/10/2011        18:33:03                6
> 54            52 28/10/2011        15:19:47                1
> 55            41 28/10/2011        19:00:24                8
> 56            17 03/12/2011        17:38:08                2
> 57            17 03/12/2011        17:38:49               20
> 58            17 20/05/2012        19:45:17               17
> 59            17 20/05/2012        19:45:58               47
> 60            17 22/05/2012        23:12:54                1
> 61            17 22/05/2012        23:15:15                3
> 62            17 22/05/2012        23:15:40                7
> 63            17 22/05/2012        23:16:09               40
> 64            17 22/05/2012        23:36:30                3
> 65            17 22/05/2012        23:37:29               10
> 66            17 22/05/2012        23:39:17                1
> 67            17 23/05/2012        20:44:01               15
> 68             6 17/07/2012        04:25:29                8
> 69            53 17/07/2012        23:53:01                2
> 70            53 24/07/2012        03:09:25                1
> 71            17 24/07/2012        20:58:42               16
> 72            17 24/07/2012        21:00:46               13
> 73            17 25/07/2012        04:57:54               21
> 74            17 25/07/2012        05:04:37                1
> 75            17 27/07/2012        00:53:59              125
> 76            17 27/07/2012        01:00:55                1
> 77            17 27/07/2012        01:01:57                1
> 78            17 27/07/2012        01:28:46                1
> 79            17 27/07/2012        01:41:14                5
> 80            17 27/07/2012        01:42:18                1
> 81            17 27/07/2012        01:43:46                1
> 82            17 27/07/2012        01:49:23                4
> 83            17 27/07/2012        04:26:49                1
> 84            17 27/07/2012        04:44:38                3
> 85            17 27/07/2012        04:45:27                1
> 86            17 27/07/2012        05:21:40                2
> 87            17 27/07/2012        05:22:13                2
> 88            17 27/07/2012        05:39:47                1
> 89            17 28/07/2012        00:11:46                3
> 90            17 30/07/2012        20:58:47                1
> 91            17 30/07/2012        20:59:22                1
> 92            17 30/07/2012        20:59:50                1
> 93            17 30/07/2012        21:03:15                1
> 94            17 30/07/2012        21:04:00               10
> 95            17 30/07/2012        21:05:05                1
> 96            17 30/07/2012        21:15:02                1
> 97            17 30/07/2012        22:04:55                1
> 98            17 30/07/2012        22:27:21                1
> 99            17 30/07/2012        22:33:12                5
> 100           17 30/07/2012        22:35:55                3
> 101           17 30/07/2012        22:36:32                4
> 102           17 30/07/2012        22:38:38               37
> 103           17 30/07/2012        22:40:45                6
> 104           17 30/07/2012        22:43:13                2
> 105           17 30/07/2012        22:46:56                3
> 106           17 30/07/2012        22:54:33               29
> 107           17 30/07/2012        23:12:10                1
> 108           17 30/07/2012        23:12:36               25
> 109           17 30/07/2012        23:14:42                1
> 110           17 30/07/2012        23:20:08                1
> 111           17 30/07/2012        23:29:27               11
> 112           17 30/07/2012        23:35:28                1
> 113           17 30/07/2012        23:36:44                1
> 114           17 30/07/2012        23:43:15                1
> 115           17 30/07/2012        23:43:49                1
> 116           17 30/07/2012        23:44:39               29
> 117           17 30/07/2012        23:45:31                1
> 118           17 30/07/2012        23:52:18               23
> 119           17 30/07/2012        23:53:20                2
> 120           17 30/07/2012        23:53:57               12
> 121           17 30/07/2012        23:54:37               19
> 122           17 30/07/2012        23:59:34               19
> 123           17 31/07/2012        00:13:14                5
> 124           17 31/07/2012        00:13:44                8
> 125           17 31/07/2012        00:16:08               11
> 126           17 31/07/2012        00:16:58               39
> 127           17 31/07/2012        00:18:16               13
> 128           17 31/07/2012        00:19:43               12
> 129           17 31/07/2012        00:20:26                7
> 130           17 31/07/2012        00:21:05                1
> 131           17 31/07/2012        00:27:05                4
> 132           17 31/07/2012        00:54:22                1
> 133           17 31/07/2012        00:54:48               96
> 134           17 31/07/2012        01:01:57                1
> 135           17 31/07/2012        01:47:05                1
> 136           17 31/07/2012        01:47:50               20
> 137           17 31/07/2012        01:48:32                8
> 138           17 31/07/2012        03:37:37                1
> 139           17 31/07/2012        03:38:03                5
> 140           17 31/07/2012        03:38:44               99
> 141           17 31/07/2012        03:41:34               28
> 142           17 31/07/2012        03:43:49               22
> 143           17 31/07/2012        03:45:43                1
> 144           17 31/07/2012        03:49:19               24
> 145           17 31/07/2012        04:03:03                6
> 146           17 31/07/2012        04:06:10               65
> 147           17 31/07/2012        04:11:19                1
> 148           17 31/07/2012        04:11:49                1
> 149           17 31/07/2012        04:24:48                3
> 150           17 31/07/2012        04:25:39               80
> 151           17 31/07/2012        04:27:28               17
> 152           17 31/07/2012        04:32:41                7
> 153           17 31/07/2012        04:45:24                1
> 154           17 31/07/2012        04:56:40                6
> 155           17 31/07/2012        05:05:56                2
> 156           17 31/07/2012        05:07:29                1
> 157           17 01/08/2012        00:21:25                1
> 158           17 01/08/2012        00:22:17               25
> 159           17 01/08/2012        00:23:36               12
> 160           17 01/08/2012        00:25:14                4
> 161           17 01/08/2012        00:41:13                1
> 162           17 01/08/2012        00:42:28               12
> 163           17 01/08/2012        00:43:54                2
> 164           17 01/08/2012        00:55:37                8
> 165           17 01/08/2012        01:00:38               39
> 166           17 01/08/2012        01:06:32                9
> 167           17 01/08/2012        01:11:19                1
> 168           17 01/08/2012        01:14:10                3
> 169           17 01/08/2012        01:14:48               17
> 170           17 01/08/2012        01:15:30               27
> 171           17 01/08/2012        01:16:20               71
> 172           17 01/08/2012        01:18:05              107
> 173           17 01/08/2012        01:23:54                1
> 174           17 01/08/2012        01:24:51               11
> 175           17 01/08/2012        01:32:09                1
> 176           17 01/08/2012        01:32:57               16
> 177           17 01/08/2012        01:33:35               69
> 178           17 01/08/2012        01:38:47                1
> 179           17 01/08/2012        01:39:18               75
> 180           17 01/08/2012        01:41:07                1
> 181           17 01/08/2012        01:41:48                1
> 182           17 01/08/2012        01:53:50                1
> 183           17 01/08/2012        01:55:29                1
> 184           17 01/08/2012        01:55:52                2
> 185           17 01/08/2012        01:56:17               25
> 186           17 01/08/2012        02:01:00                3
> 187           17 01/08/2012        02:03:13               32
> 188           17 01/08/2012        02:04:34               99
> 189           17 01/08/2012        02:06:43               43
> 190           17 01/08/2012        02:07:58                7
> 191           17 01/08/2012        02:08:55               22
> 192           17 01/08/2012        02:09:47                6
> 193           17 01/08/2012        02:20:21               15
> 194           17 01/08/2012        04:06:42                1
> 195           17 01/08/2012        04:15:26               46
> 196           17 01/08/2012        04:21:12                1
> 197           17 01/08/2012        04:22:12                1
> 198           17 01/08/2012        04:34:28               25
> 199           17 01/08/2012        04:35:26                1
> 200           17 01/08/2012        04:36:01               14
> 
> When an animal was caught and the collar battery needed to be changed,
> a whole new collar had to be put on, as
> 
> these animals (cows, pigs, wild boar and red deer!) were not that easy
> to catch, or hold onto for long! In order to follow the movements of
> each animal rather than each collar I now need to create a new column
> that assigns the data by animal rather than by collar. I have a table
> of dates showing which animal was wearing which collar and when:
> 
>> head(allocate)
>   animal collar start_date   end_date
> 1    CO1     36 16/07/2010 11/04/2011
> 2    CO1     64 12/04/2011 21/02/2012
> 3    CO1     25 21/02/2012 10/07/2012
> 4    CO2     28 16/07/2010 09/04/2011
> 5    CO2     11 12/04/2011 10/04/2012
> 6    CO3     46 16/07/2010 10/04/2011
> 
>> dput(allocate)
> structure(list(animal = structure(c(1L, 1L, 1L, 10L, 10L, 11L,
> 11L, 11L, 12L, 13L, 13L, 14L, 15L, 16L, 16L, 16L, 17L, 17L, 17L,
> 2L, 3L, 4L, 4L, 4L, 5L, 6L, 6L, 7L, 8L, 9L, 9L, 26L, 26L, 26L,
> 31L, 31L, 31L, 31L, 32L, 32L, 32L, 32L, 33L, 33L, 33L, 34L, 34L,
> 34L, 35L, 35L, 36L, 36L, 37L, 38L, 38L, 27L, 27L, 28L, 28L, 29L,
> 29L, 29L, 30L, 30L, 30L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
> 39L, 40L, 41L), .Label = c("CO1", "CO10", "CO11", "CO12", "CO13",
> "CO14", "CO15", "CO16", "CO17", "CO2", "CO3", "CO4", "CO5", "CO6",
> "CO7", "CO8", "CO9", "PI1", "PI2", "PI3", "PI4", "PI5", "PI6",
> "PI7", "PI8", "PO1", "PO10", "PO11", "PO12", "PO13", "PO2", "PO3",
> "PO4", "PO5", "PO6", "PO7", "PO8", "PO9", "RD1", "RD2", "WB1"
> ), class = "factor"), collar = c(36L, 64L, 25L, 28L, 11L, 46L,
> 58L, 12L, 4L, 52L, 2L, 63L, 14L, 12L, 12L, 57L, 51L, 51L, 24L,
> 42L, 44L, 25L, 23L, 7L, 2L, 45L, 9L, 11L, 30L, 14L, 42L, 23L,
> 19L, 52L, 41L, 53L, 18L, 41L, 55L, 10L, 15L, 50L, 34L, 3L, 16L,
> 32L, 16L, 37L, 50L, 19L, 37L, 3L, 10L, 27L, 41L, 6L, 15L, 62L,
> 18L, 52L, 39L, 52L, 62L, 53L, 62L, 18L, 63L, 37L, 57L, 54L, 23L,
> 56L, 38L, 17L, 61L, 33L), start_date = structure(c(14L, 7L, 22L,
> 14L, 7L, 14L, 7L, 3L, 7L, 14L, 13L, 11L, 14L, 14L, 16L, 15L,
> 14L, 13L, 15L, 29L, 14L, 14L, 13L, 15L, 14L, 14L, 15L, 6L, 6L,
> 13L, 12L, 31L, 6L, 18L, 29L, 6L, 25L, 10L, 30L, 6L, 23L, 10L,
> 28L, 4L, 22L, 30L, 6L, 22L, 6L, 20L, 24L, 22L, 27L, 2L, 23L,
> 24L, 10L, 27L, 9L, 6L, 27L, 8L, 6L, 23L, 9L, 17L, 5L, 17L, 1L,
> 17L, 1L, 1L, 17L, 21L, 19L, 26L), .Label = c("02/06/2011", "02/11/2010",
> "09/02/2012", "10/04/2011", "10/12/2010", "11/04/2011", "12/04/2011",
> "13/02/2012", "14/02/2012", "15/02/2012", "15/07/2010", "16/02/2012",
> "16/03/2011", "16/07/2010", "17/02/2012", "17/03/2011", "18/10/2010",
> "19/07/2011", "20/06/2011", "20/07/2011", "20/08/2011", "21/02/2012",
> "21/07/2011", "21/09/2011", "22/09/2011", "23/07/2011", "25/07/2011",
> "25/08/2010", "26/10/2010", "28/10/2010", "29/08/2010"), class =
> "factor"),
>     end_date = structure(c(18L, 34L, 16L, 12L, 15L, 14L, 29L,
>     41L, 32L, 7L, 19L, 17L, 24L, 36L, 26L, 30L, 36L, 31L, 27L,
>     11L, 36L, 36L, 26L, 28L, 9L, 20L, 41L, 15L, 25L, 24L, 10L,
>     40L, 29L, 5L, 11L, 33L, 4L, 28L, 18L, 29L, 1L, 22L, 14L,
>     1L, 30L, 18L, 24L, 30L, 33L, 30L, 23L, 30L, 30L, 37L, 1L,
>     4L, 30L, 4L, 30L, 33L, 4L, 30L, 33L, 4L, 30L, 3L, 39L, 42L,
>     6L, 38L, 2L, 21L, 8L, 35L, 35L, 13L), .Label = c("01/02/2012",
>     "02/06/2011", "03/02/2011", "03/02/2012", "03/05/2012", "03/06/2011",
>     "04/01/2011", "04/02/2011", "05/03/2011", "05/06/2012", "07/04/2011",
>     "09/04/2011", "10/02/2012", "10/04/2011", "10/04/2012", "10/07/2012",
>     "10/12/2010", "11/04/2011", "12/10/2011", "13/01/2011", "13/02/2012",
>     "13/11/2012", "14/02/2012", "15/02/2012", "16/01/2012", "17/02/2012",
>     "18/05/2012", "18/07/2012", "19/07/2011", "19/07/2012", "19/10/2011",
>     "20/06/2011", "20/07/2011", "21/02/2012", "21/10/2012", "23/02/2011",
>     "23/03/2011", "24/01/2011", "26/01/2011", "28/01/2011", "29/03/2012",
>     "30/05/2011"), class = "factor")), .Names = c("animal", "collar",
> "start_date", "end_date"), class = "data.frame", row.names = c(NA,
> -76L))
> 
> 
> I have so far been able to make some conditional tests, though I would
> need to do this for all 76 animals in the
> 
> study...
> 
> animal1test<- (date>=01/01/13 && date<=03/01/13)
> animal1test2<- (date>=04/01/13 && date<=06/01/13)
> animal2test<- (date>=04/01/13 && date<=06/01/13)
> 
> to use in a giant'if else' formula:
> 
>  if(animal1test){
>   collar[1]="animal1"
>   } else if(animal1test2){
>     collar[5]="animal1"
>   }else if(animal2test)
>     collar[1]="animal2"
>     }else "NA"
> 
> ...etc...
> 
> As I'm sure you can see, this is completely inelegant, and also not
> working for me! Any ideas on how to a achieve
> 
> this?
> 
> Thanks,
> Cat
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails



More information about the R-help mailing list