[R] Unique observations

David Winsemius dwinsemius at comcast.net
Wed Nov 25 18:49:39 CET 2009


On Nov 25, 2009, at 9:44 AM, John Lipkins wrote:

> Hey R list,
>
> A beginners question. How can I do the following:
>
> In my research population it is possible that several items can appear
> several times, measured on different moments in time. This is being  
> supplied
> in a total list with all observations identified by a number (per  
> item) and
> a moment of observation (date). Now I want to make a unique list of  
> this
> observation preserving the characteristics of the first observation.  
> As
> example:
>
> Tree  disease  date
> Tree1  leaves  01-01-2009
> Tree2  roots  13-09-2009
> Tree1  roots  24-10-2009
>
> Now I want to create a list of unique elements (in the example only  
> once
> Tree1 and Tree2) with the first observed disease and date. For the  
> example
> the result would look like:
>
> Tree  disease  date
> Tree1  roots  24-10-2008
> Tree2  roots  13-09-2009
>
> Can someone help me with this question?

I think the function you will need include:

?as.Date  #to get the date field into sortable form
?order    #as in treedat2 <- treedat[ order(treedat$dt, treedat$Tree), ]
?duplicated  # as in treedat2[ !duplicated(treedat2(Tree), ]

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list