Randomly select one data entry for the morning and one for the evening for each day in R I have a data set of recorded elephant locations for the span of 6 years (elephantdata). Some of the entries are duplicates of the same day. I want to create a subset of my 'elephantdata' and create a new matrix that only has one data point before noon and one data point after noon for each day. I would like that data point to be selected randomly from the data points offered.
For example:
elephantdata
Row     Date       Time    Breeding_H   E1  E2  E3
 1     11/01/06    08:38    1   0   0   0
 2     11/01/06    18:00    1   0   0   0
 3     12/01/06    05:38    1   0   0   0
 4     02/02/06    08:58    1   0   0   0
 5     02/02/06    16:30    1   1   1   1
 6     02/02/06    17:15    1   1   1   1
 7     03/02/06    08:45    1   0   0   0

For Breeding_H I want to find that if the 'Date' is a duplicate AND two entries are in the morning (before 12:00) or in the evening (after noon) I want R to randomly chose one of the entries. So Row 6 and 7 are both after noon and on the same day, so I would like to only have one of those data entries.
Any suggestions? Would really appreciate any help you've got. I've looked up the 'unique' and 'duplicated' function but I don't know how to write the actual code to make this happen. 
 A: Write a function of your date and time variables that identifies the individual half-day groups to categorize your subsets, supply that to tapply and use sample on the subsets.
So imagine you already had your half-day groups, like so (here I have three subsets called A B and C):
> mydata
   grp   dat
1    A  0.17
2    C -0.39
3    A -0.87
4    C -0.79
5    C  2.66
6    B  0.76
7    B -1.34
8    A  0.66
9    A -2.32
10   A -1.24
11   A -1.43
12   C -0.06
13   C -0.81

and we wanted one random value from each of the subgroups A, B and C (the groups don't need to explicitly exist in the data, you can just have an expression in the second argument to tapply that gives a factor which does the job):
> with(mydata,tapply(dat,grp,sample,size=1))
    A     B     C 
-0.87 -1.34 -0.81 

So you just need to use tapply in a similar fashion on your half-day subsets.
You don't need to check if there's one value or two values (if there's one it will be picked for sure, if there's more than one it will just choose one at random), and the code doesn't break if you somehow get 3 or 4 values in one afternoon.
